How to scroll down using Selenium WebDriver with Java?

Software TestingAutomation TestingSelenium Web DriverJava

We can scroll down with Selenium. Selenium is unable to handle scrolling directly. It takes the help of the Javascript Executor to perform the scrolling action up to an element.

First of all we have to locate the element up to which we have to scroll to. Next, we shall use the Javascript Executor to run the Javascript commands. The method executeScript is used to run Javascript commands in Selenium. We shall take the help of the scrollIntoView method in Javascript and pass true as an argument to the method.


WebElement elm = driver.findElement("name"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);",elm);


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
public class ScrollAction{
   public static void main(String[] args) {
      WebDriver driver = new ChromeDriver();
      driver.get(" ");
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // identify element
      WebElement n=driver.findElement(By.xpath("//*[text()='Contact']"));
      // Javascript executor
      (true);", n);


Published on 30-Nov-2020 15:23:44