How to scroll down the page till page end in the Selenium WebDriver?

SeleniumAutomation TestingTesting Tools

We can scroll down the page till page end with Selenium webdriver.Selenium cannot directly scroll up or down with its methods. This is done with the Javascript Executor. DOM can interact with the elements with Javascript.

Selenium runs the commands in Javascript with the execute_script() method. For scrolling till the page down, we have to pass (0, document.body.scrollHeight) as parameters to the method scrollTo().

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;

public class ScrollTillPageEnd{
   public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      // Javascript executor
      ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
      driver.quit();
   }
}

Output

raja
Published on 18-Sep-2020 17:30:58
Advertisements