Multiple Ways to Scroll a Page Using Selenium WebDriver

Multiple Ways to Scroll a Page Using Selenium WebDriver

There are a number of ways to scroll a page using Selenium WebDriver in Java. Some of them are discussed below.

  1. If you want to scroll the page vertically to perform some action, you can do it using the following JavaScript.

        ((JavascriptExecutor)

driver).executeScript(“window.scrollTo(0,

document.body.scrollHeight)”);

            Where ‘JavascriptExecutor’ is an interface, which helps executing JavaScript through Selenium WebDriver. You can use the following code to import.

import org.openqa.selenium.JavascriptExecutor; 

2.      If you want to scroll at a particular element, you need to use the following JavaScript.

WebElement element =

driver.findElement(By.xpath(“//input [@id=’email’]”));

((JavascriptExecutor)

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

Where ‘element’ is the locator where you want to scroll.

3.      If you want to scroll at a particular coordinate, use the following JavaScript.  

((JavascriptExecutor)

driver).executeScript(“window.scrollBy(200,300)”);

Where ‘200,300’ are the coordinates.

  1. If you want to scroll up in a vertical direction, you can use the following JavaScript.

((JavascriptExecutor)

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

  1. If you want to scroll horizontally in the right direction, use the following JavaScript.

((JavascriptExecutor)

driver).executeScript(“window.scrollBy(2000,0)”);

  1. If you want to scroll horizontally in the left direction, use the following JavaScript.

((JavascriptExecutor)

driver).executeScript(“window.scrollBy(-2000,0)”);

 

SHARE THIS

Get A Free Quote

4 thoughts on “Multiple Ways to Scroll a Page Using Selenium WebDriver

  1. Helpful article, and i need how handle google popup,java script toast message and some confirmation messages at a time. please help me

  2. This generally looks like a visual testing task. Are you limited to Selenium only? In case there is an option for you to try something else for this task, you could do that with a visual regression testing tool. Such as Screenster (http://screenster.io/) which is actually an alternative to Selenium for visual/CSS testing tasks.

    Screenster is a test automation tool which performs screenshot-based comparison of different versions of your web pages. First it creates a visual baseline for a page, taking a screenshot for each user action. During the next run it takes a new screenshot at each step, compares it with the one from baseline and highlights differences. It also has a number of features for easy maintenance of tests.

  3. Hi,

    I notice that you have used an image that I created to promote and brand my online training in Selenium WebDriver. Can you please use a different image so that people do not confuse your Selenium WebDriver material with my Selenium WebDriver material.

    Thanks,

    Alan

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.