Key takeaways
- Selenium 4.2 and later scroll natively with wheel actions; the JavaScript executor works in every version.
- scrollIntoView is more reliable than pixel offsets, which break on different viewports.
- Scroll before interacting with anything below the fold or the click may miss.
If you want to scroll the page UP or DOWN with Selenium WebDriver in order to find web elements or make assertions, here are a few way you can do that.
How to scroll Page Down using Selenium WebDriver:
JavaScript scrollBy() method scrolls the document by the specified number of pixels. Update the second parameter of the scrollBy() method with the number of pixels in order scroll more or less.
The definition of the JavaScript method scrollBy() is:
window.scrollBy(xpixels,ypixels)
where:
- xpixels is a Number and Required parameter:
- How many pixels to scroll by, along the x-axis (horizontal).
- Positive values will scroll to the right, while negative values will scroll to the left
- ypixels is a Number and Required parameter:
- How many pixels to scroll by, along the y-axis (vertical).
- Positive values will scroll down, while negative values scroll up
In the examples we are using 250 pixels for vertical scrolling in order to show how scroll method works.
@Test(groups = {"smoke"})
public void test_Scroll_Page_Down() throws Exception {
init();
driver.navigate().to("https://en.wikipedia.org/wiki/Software_testing");
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,250)", "");
}
How to scroll Page UP using Selenium WebDriver:
Similar to scrolling Down the web page, but we set a negative number of pixels as the second parameter of the JS scrollBy() method to a negative number: -250
@Test(groups = {"smoke"})
public void test_Scroll_Page_UP() throws Exception {
init();
driver.navigate().to("https://en.wikipedia.org/wiki/Software_testing");
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,-250)", "");
}
How to scroll to the Bottom of the Web Page using Selenium WebDriver:
Basically, we get the height of the Body element from the DOM (Document Object Model) and we use the JavaScript method scrollTo() to scroll to the maximum height of the page:
@Test(groups = {"smoke"})
public void test_Scroll_Page_To_Bottom() throws Exception {
init();
driver.navigate().to("https://en.wikipedia.org/wiki/Software_testing");
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
}
How to scroll Page to a Web Element Selenium WebDriver:
Just identify the Web Element fro the webpage and use the JS method scrollIntoView() to scroll the page to that web element until it becomes visible
@Test(groups = {"smoke"})
public void test_Scroll_Page_To_Element() throws Exception {
init();
driver.navigate().to("https://en.wikipedia.org/wiki/Software_testing");
JavascriptExecutor jse = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.linkText("Regression testing"));
jse.executeScript("arguments[0].scrollIntoView();", element);
}
How to scroll Page Down or UP using Keys in Selenium WebDriver:
If you want to use the keys from the keyboard to scroll UP or DOWN you can use the Actions sendKeys() method and pass PAGE_UP or PAGE_DOWN as an argument. Call perform() to send the key press before you wait; an Actions chain does nothing until it is performed:
@Test(groups = {"smoke"})
public void test_Scroll_Page_Using_Keys() throws Exception {
init();
driver.navigate().to("https://en.wikipedia.org/wiki/Software_testing");
new Actions(driver).sendKeys(Keys.PAGE_DOWN).perform();
waitSeconds(2);
driver.findElement(By.partialLinkText("Regression testing")).click();
}
How to scroll with Selenium 4 wheel actions:
Since Selenium 4.2, the Actions API can scroll without JavaScript: scrollToElement brings an element into the viewport and scrollByAmount scrolls by a number of pixels, with the same sign rules as scrollBy (selenium.dev wheel actions documentation, November 2026).
@Test(groups = {"smoke"})
public void test_Scroll_With_Wheel_Actions() throws Exception {
init();
driver.navigate().to("https://en.wikipedia.org/wiki/Software_testing");
WebElement element = driver.findElement(By.linkText("Regression testing"));
new Actions(driver).scrollToElement(element).perform();
new Actions(driver).scrollByAmount(0, 250).perform();
}
The original examples in this post used alexa.com/topsites, which Amazon retired in 2022, so they now load a Wikipedia article instead. Hope you’ll find this useful. Check also how to find web elements using Selenium WebDriver.
LoadFocus is a cloud load testing platform. Try it for free.
Frequently Asked Questions
Does Selenium have a scroll command?
Yes, since Selenium 4.2: the Actions API has scrollToElement and scrollByAmount. In older versions, scrolling goes through the JavaScript executor, using window.scrollBy for pixel movement or scrollIntoView to bring a specific element into the viewport.
Should I scroll by pixels or to an element?
To the element. A pixel offset that works on your screen lands somewhere else on a different viewport or a different amount of content, whereas scrollIntoView finds the element wherever it is.
Do I need to scroll before clicking?
For anything below the fold, yes. An element outside the viewport can be located but not reliably clicked, and the failure looks like a bad locator rather than a scroll problem.