2 minutes read

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.

Are Your APIs as Reliable as You Think?

Don’t let hidden issues disrupt your service. With LoadFocus’s advanced API Monitoring, catch problems before they impact your users. Ensure flawless performance and avoid costly outages—monitor, test, and optimize your APIs effortlessly.

Explore Pricing
Instant alerts
Learn More About API Monitoring
Seamless integration

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 left, while negative values will scroll to the right
  • 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.

Think your website can handle a traffic spike?

Fair enough, but why leave it to chance? Uncover your website’s true limits with LoadFocus’s cloud-based Load Testing for Web Apps, Websites, and APIs. Avoid the risk of costly downtimes and missed opportunities—find out before your users do!

Effortless setup No coding required



@Test(groups = {"smoke"})
public void test_Scroll_Page_Down() throws Exception {
init();

driver.navigate().to("http://www.alexa.com/topsites/countries;15/LU");

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("http://www.alexa.com/topsites/countries;15/LU");

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("http://www.alexa.com/topsites/countries;15/LU");

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("http://www.alexa.com/topsites/countries;15/LU");

JavascriptExecutor jse = (JavascriptExecutor) driver;

WebElement element = driver.findElement(By.linkText("Google.com.ph"));
jse.executeScript("arguments[0].scrollIntoView();", element);
}

LoadFocus is an all-in-one Cloud Testing Platform for Websites and APIs for Load Testing, Apache JMeter Load Testing, Page Speed Monitoring and API Monitoring!

Effortless setup No coding required

How to scroll Page Down or UP using Keys Selenium WebDriver:

If you want to use the keys form the keyboard to scroll UP or DOWN you can use the WebDriver sendKeys() method and pass the PAGE_UP or PAGE_DOWN as an argument:


@Test(groups = {"smoke"})
public void test_Scroll_Page_Using_Keys() throws Exception {
init();


driver.navigate().to("http://www.alexa.com/topsites/countries;15/LU");

Actions action = new Actions(driver);
action.sendKeys(Keys.PAGE_DOWN);
waitSeconds(2);
action.click(
    driver.findElement(By.partialLinkText("Google.com.ph"))
).perform();
}

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.

How fast is your website? Free Website Speed Test