Multiple Ways to Scroll a Page Using Selenium WebDriver

There are a number of ways to scroll a page using Selenium WebDriver in Java. Some of them are discussed below.

  1. If you want to scroll the page vertically to perform some action, you can do it using the following JavaScript.

        ((JavascriptExecutor)

driver).executeScript(“window.scrollTo(0,

document.body.scrollHeight)”);

            Where ‘JavascriptExecutor’ is an interface, which helps executing JavaScript through Selenium WebDriver. You can use the following code to import.

import org.openqa.selenium.JavascriptExecutor; 

2.      If you want to scroll at a particular element, you need to use the following JavaScript.

WebElement element =

driver.findElement(By.xpath(“//input [@id=’email’]”));

((JavascriptExecutor)

driver).executeScript(“arguments[0].scrollIntoView();”, element);

Where ‘element’ is the locator where you want to scroll.

3.      If you want to scroll at a particular coordinate, use the following JavaScript.  

((JavascriptExecutor)

driver).executeScript(“window.scrollBy(200,300)”);

Where ‘200,300’ are the coordinates.

  1. If you want to scroll up in a vertical direction, you can use the following JavaScript.

((JavascriptExecutor)

driver).executeScript(“window.scrollTo(document.body.scrollHeight,0)”);

  1. If you want to scroll horizontally in the right direction, use the following JavaScript.

((JavascriptExecutor)

driver).executeScript(“window.scrollBy(2000,0)”);

  1. If you want to scroll horizontally in the left direction, use the following JavaScript.

((JavascriptExecutor)

driver).executeScript(“window.scrollBy(-2000,0)”);