Posted

Reading Time: 2 minutes

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 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.




@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

<code class="lang-java">
@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);
}

<h3>How to scroll Page Down or UP using Keys Selenium WebDriver:</h3> If you want to use the keys form the keyboard to<strong> scroll UP or DOWN</strong> you can use the WebDriver sendKeys() method and pass the <strong>PAGE_UP</strong> or <strong>PAGE_DOWN</strong> as an argument: <pre class="lang-java"><code class="lang-java"> @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.





Thanks for reading. I hope you learned something interesting about React!
If you found this article useful, please share it with others. Don’t forget to subscribe to get notified for the upcoming articles.