---Advertisement---

How to Scroll a Web Page in Selenium WebDriver using JavaScriptExecutor Great 2025

By Manisha

Updated On:

---Advertisement---
How to Scroll a Web Page in Selenium

How to Scroll a Web Page in Selenium: In web automation testing, sometimes web elements are not immediately visible on the page due to their position. To interact with such elements, scrolling becomes necessary. Selenium WebDriver offers a powerful way to perform scroll operations using the JavaScriptExecutor interface.


How to Scroll a Web Page in Selenium: Selenium WebDriver interacts with the Document Object Model (DOM) directly and doesn’t require scrolling in most cases. However, when elements are hidden due to layout or dynamic loading, manual scrolling via JavaScript is essential.


How to Scroll a Web Page in Selenium: JavaScriptExecutor is an interface in Selenium that allows executing JavaScript commands directly in the browser context.

βœ… Syntax:

java

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(“script”, args);

  • script: JavaScript code to execute
  • args (optional): DOM elements or parameters used in the script

  1. Scroll by Pixel
  2. Scroll by Element Visibility
  3. Scroll to Bottom of Page
  4. Horizontal Scroll
  5. Scrollbar Types Explained
  6. Summary

βœ… Description:

Use the scrollBy() method to scroll vertically by a specific number of pixels.

βœ… JavaScript Syntax:

javascript

window.scrollBy(x, y);

  • x: horizontal pixels (positive = right, negative = left)
  • y: vertical pixels (positive = down, negative = up)

βœ… Selenium Example:

java

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(“window.scrollBy(0,1000)”);

Output:

The browser window scrolls down by 1000 pixels.


βœ… Description:

How to Scroll a Web Page in Selenium: Use scrollIntoView() to scroll the page until the specified element is fully visible in the viewport.

βœ… Selenium Example:

java

WebElement element = driver.findElement(By.id(“elementId”));

JavascriptExecutor js = (JavascriptExecutor) driver;

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

  • “arguments[0]”: refers to the element passed to the script
  • scrollIntoView(true): aligns the element to the top of the viewport

Output:

The page scrolls until the element becomes visible.


βœ… Description:

How to Scroll a Web Page in Selenium: Use scrollTo() and document.body.scrollHeight to scroll to the very end of the page.

βœ… Selenium Example:

java

JavascriptExecutor js = (JavascriptExecutor) driver;

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

Output:

The browser scrolls to the bottom of the page.


βœ… Description:

Scroll horizontally to bring wide page elements into view using scrollIntoView().

βœ… Selenium Example:

java

WebElement element = driver.findElement(By.id(“horizontalElement”));

JavascriptExecutor js = (JavascriptExecutor) driver;

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

Output:

The browser scrolls horizontally until the target element is visible.


A scrollbar enables movement along the page when the content exceeds the visible viewport. There are two types:

  • Vertical Scrollbar – scrolls up and down
  • Horizontal Scrollbar – scrolls left and right

Selenium does not require scrollbars for most interactions, but scroll actions may be necessary for dynamic elements that appear only after scrolling.


ScenarioDescription
Scroll by PixelScrolls the page by specific pixel values
Scroll to Element VisibilityScrolls until the target element is visible
Scroll to Page BottomScrolls to the bottom using document height
Horizontal ScrollScrolls horizontally to bring elements into view

  • JavaScriptExecutor allows scrolling using native JS methods.
  • Use scroll actions only when required (lazy loading, dynamic content).
  • Scrolling enhances test stability when elements aren’t initially visible.

Testing Selenium Download

Object Repository in Selenium

Leave a Comment

Index