How to Perform Scroll Down Operation Using Selenium Webdriver

Jash Unadkat
1 min readMay 29, 2020

Selenium WebDriver is capable of manipulating the Document Object Model (DOM), and hence it doesn’t require scroll to perform certain actions. However, in some cases, there are certain web elements (for example, a submit button) that become visible only once the user has scrolled down. In such cases, automating the scroll operation becomes necessary.

A Scroll is a JavaScript method. The JavaScript Executor provides an interface that enables QAs to run JavaScript methods from Selenium scripts

Now let’s discuss how to scroll down or up the webpage by a specific number of pixels?

Refer to the Selenium script below, for performing a scroll down action on the Firefox browser.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HandleScroll
{

@Test
public void scrollDown()
{
System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("Website URL");

//to perform Scroll on application using Selenium
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,350)", "");
}
}

Output: The code initializes Gecko driver for Firefox. Then the Firefox browser is launched, and it navigates to the specified website URL. Once the website loads, the browser window is vertically scrolled down by 350 pixels.

If a user needs to scroll up, they just need to modify the pixel value of the second parameter (in this case 350) to negative value (-350). The rest code remains completely the same.

For more understanding detailed operations like how to scroll to the bottom of page, how to scroll horizontally, refer to this detailed guide on Selenium Scroll tutorial which includes detailed execution of Selenium Scripts.

--

--

Jash Unadkat

As a tech geek, I love writing articles about everything related to web development or software testing space.