How to scroll down a webpage in Selenium using Java?

How to scroll down a webpage in Selenium using Java?

Hello Ana-sousa,

In order to scroll the web page you can use JavascriptExecutor.

Create an object for JavascriptExecutor and call the WebDriver. Then execute the script with a scrollTo function and using that you can either scroll to a particular section or to the bottom of your page.

Here is the script for doing this:

JavascriptExecutor js = ((JavascriptExecutor) driver); js.executeScript(“window.scrollTo(0, document.body.scrollHeight)”); Thread.sleep(5000);

We are using thread.sleep to give the browser a time to scroll if you have used driver.close(); then the browser may close before scroll takes place and you may not see the result.