Advance Selenium

How to Scroll Page in Selenium Webdriver using Java Script

Hello, Welcome to Selenium tutorials, in this post we will see Scroll page in Selenium Webdriver using different ways.

JavaScript plays very important role in Selenium Webdriver too, earlier also we have seen how to work with JavaScript and usage of Java Script in Selenium Webdriver.

We can highlight elements as well using JavaScript.

If you are working with Mobile Test Automation using Appium then in mobile devices we have pre-defined method to scroll page

 

Scroll page in Selenium Webdriver

 

 

Before starting, I want to share one more point which will clear your doubts. We have two type of scroll in Selenium.

1- Scroll into view (Kindly check detailed post which covers the complete scenario)

2- Scroll page (Scroll page and Scroll down)

 

Selenium handle scrolling page automatically but if want to scroll page using Selenium then yes we can do easily using JavaScript.

We have method scroll(horizontal, vertical) i.e. scroll(0,400)

 

Note- scroll method is not a method of Webdriver, this is a method of JavaScript. 

 

Scroll page in Selenium Webdriver

To execute JavaScript code inside Selenium script we can take the help of JavascriptExecutor

JavascriptExecutor is an Interface which is available in  package org.openqa.selenium.JavascriptExecutor;

Inside this Interface we have a method called executeScript()– so whatever script you will pass as a string It will be executed by JavascriptExecutor.

 

Recently I have published video for the same. 

 

 

Program to Scroll page in Selenium Webdriver.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class ScrollTestCase {

 public static void main(String[] args) throws Exception {
  
 // load browser
  WebDriver driver=new FirefoxDriver();
 

 // maximize browser
  driver.manage().window().maximize();
 

  // Open Application
  driver.get("http://jqueryui.com");
  

  // Wait for 5 second
  Thread.sleep(5000);

 // This  will scroll page 400 pixel vertical
  ((JavascriptExecutor)driver).executeScript("scroll(0,400)");
  
     
 }

}

 

Please comment in below section if you have any issue while executing above code

Thanks for visiting my blog. Keep in touch.

Happy Testing. 🙂

 

For more updates Like our Facebook Page.

For any Selenium related query Join Facebook Group.

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

34 thoughts on “How to Scroll Page in Selenium Webdriver using Java Script

  1. Vetrivelan says:

    Great article Mukesh. Thanks for sharing the same.

    1. Hi Vetrivelan,

      Very very thanks for your comments.

  2. sreedhar says:

    Hi Mukesh,

    ((JavascriptExecutor)driver).executeScript(“scroll(0,400)”);.

    1.what is that indicates 0,400 exactly.
    2.How will come to know required Element is present at 400 only.

    1. Hi Sreedhar,

      It differs application to application. 400 is the Y coordinate of the element. You can find same using Point class. Or else you do scroll into view, for more details, please visit this link https://vistasadprojects.com/mukeshotwani-blogs-v2/how-to-scroll-into-view-in-selenium-webdriver/

  3. Siddharth says:

    Hi Mukesh

    Thanks for your support and wish you happy New Year

  4. NITESH JAIN says:

    Thanks Mukesh, you are helping a big test world ..great work!!

  5. Chetan Yeshi says:

    Hi Mukesh,

    I just had a simpler approach for scrolling, it did work for me. Please find the code below:

    public WebDriver selectProject() throws InterruptedException {
    Properties properties = TTXProperties.getTTxProperties();
    webElement=webDriver.findElement(By.xpath(properties.getProperty(“projectName”)));
    while (!isElementPresent(By
    .xpath(properties.getProperty(“projectName”)))) {

    webDriver.switchTo().activeElement().sendKeys(Keys.PAGE_DOWN);
    }
    Actions actions = new Actions(webDriver);
    actions.moveToElement(
    webDriver.findElement(By.xpath(properties
    .getProperty(“projectName”)))).doubleClick().perform();
    return webDriver;
    }

    public boolean isElementPresent(By locatorKey) {
    try {
    webDriver.findElement(locatorKey);
    return true;
    } catch (org.openqa.selenium.NoSuchElementException e) {
    return false;
    }

    1. Hey Chetan,

      Thanks for the new solution.

  6. HimaBindu says:

    Scrolling up or down the page is not at all working for me. Below is my code snippet :

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript(“window.scrollBy(0,1000)”, “”); //y value ‘250’ can be altered
    Thread.sleep(2000);
    List tabs = driver.findElements(By.xpath(“.//*[@id=’1′]/div/div[2]/table/tbody/tr/td/ul/span”));
    int noOftabs = tabs.size();
    System.out.println(“noOftabs is ” + noOftabs);
    for (int i = 1; i <= noOftabs; i++)
    {
    String tabName = tabs.get(i).getText();
    System.out.println("tabname is " + tabName + i);

    if (tabName.equals("Temp")) {
    WebElement check = driver.findElement(By.xpath(".//*[@id='1']/div/div[2]/table/tbody/tr/td/ul/span[" + noOftabs + "]/input"));
    System.out.println(" tabName " + tabName + "i is " + i);
    Thread.sleep(4000);
    check.click();
    System.out.println(" Selected " + check.isDisplayed());

    }
    }

    System.out.println("checkbox selected");
    Thread.sleep(4000);
    // JavascriptExecutor js = (JavascriptExecutor) driver;
    jse.executeScript("scroll(0, -250);"); //x value '250' can be altered
    Thread.sleep(2000);

    1. I use scroll method to scroll but you have used scrollBy

  7. Gaurav Khurana says:

    Thanks vertical scroll worked. But the above mentioned site did not had horizontal scroll. Can you share a site where horizontal scoll can be tested

    1. Good question let me check.

  8. rakesh says:

    Hi mukesh,
    I would like to know whether this is possible in selenium or not. Suppose i have a page which will load by scrolling (shopping portal ). How do you I come to know that list of items loaded.

    1. Yes Rakesh it is possible. ScrollIntoView will work in this case.

  9. Aswini says:

    Hi Mukesh ,

    I tried same method for scroll horizontal by passing below method

    js.executeScript(“scroll(250, 0)”);

    But it is not working for me
    can u please suggest how to proceed scroll right

    1. Hi Aswini,

      Can u try below code

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

      1. Ramya says:

        Hi Mukhesh,

        This dint work for me for horizontal scrolling 🙁 Instead I had to use the below code (somewhere i found in stack overflow) using “Actions”

        Actions dragger = new Actions(driver);

        WebElement draggablePartOfScrollbar = driver.findElement(By.xpath(“/html/body/section[2]/div/div[2]/div/div/div”));

        int numberOfPixelsToDragTheScrollbarRight = 5000;

        dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(numberOfPixelsToDragTheScrollbarRight ,0).release().perform();

        1. Thanks Ramya for update.

  10. Nivi says:

    Hi,
    I am unable to scroll down on a page on a mobile app on an android device using the above method. I got an error as “Method has not been implemented yet”. I tried using Actions but still I am getting the same error.
    I tried using swipe method but I am unable to scroll down. Can you please provide me a solution .

    MobileElement autoReorder=
    driver.findElement(By.xpath(“//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[3]/android.view.View[2]/android.view.View[1]”));
    autoReorder.click();
    JavascriptExecutor js=(JavascriptExecutor)driver;
    js.executeScript(“scrollBy(0,2500)”);

    //TouchAction action = new TouchAction((MobileDriver)driver);

    //Dimension size = driver.manage().window().getSize();
    //System.out.println(size);

    //Find swipe start and end point from screen’s with and height.
    //Find starty point which is at bottom side of screen.
    //int starty = (int) (size.height * 0.60);
    //Find endy point which is at top side of screen.
    //int endy = (int) (size.height * 0.30);
    //Find horizontal point where you wants to swipe. It is in middle of screen width.
    //int startx = size.width / 2;
    //System.out.println(“starty = ” + starty + ” ,endy = ” + endy + ” , startx = ” + startx);

    //Swipe from Bottom to Top.
    //driver.swipe(startx, starty, startx, endy, 3000);
    // Thread.sleep(2000);
    //Swipe from Top to Bottom.
    //driver.swipe(startx, endy, startx, starty, 3000);
    //Thread.sleep(2000);

      1. Nivi says:

        But scrollTo method is deprecated now. I am unable to use the swipe method for scrolling. Are there any other methods to do scrolling apart from “swipe”? I have one more question, Can we not use Actions and Javascript executor methods in appium ?

        1. Hi Nivi,

          Agree its deperecated in latest releases but if you use 3.2 then it will work. You can use Action and Java Script command if using Chrome Mobile Browser

  11. Sandip says:

    I really appreciate this Selenium Stuff its so heal full…

  12. bansalvarsha says:

    Hi Mukesh,
    Can you please tell me how to scroll an element up and down? I have a list which is having scroll bar and I need to scroll by dragging the scroll bar in that list

    1. Hi Varsha,

      Here you go Please check post and video too and let me know if you are finding any issue.

      https://vistasadprojects.com/mukeshotwani-blogs-v2/how-to-scroll-into-view-in-selenium-webdriver/

  13. Nitin says:

    This can also be executed by using js.executeScript(“window.scrollBy(0,4500)”);

  14. Thank you so much for your detailed description.description. Iti s really helpfull.

    1. Thanks Saroj 🙂 Keep visiting.

  15. Puneeth says:

    Simple and very easy one.

    1. aaa says:

      Hi Mukesh,

      Whether the above code will work for both scroll up and scroll down??

      1. Nitin says:

        Hi aaa,

        Yes this will work for both scroll up and scroll down but you just need to specify the correct ‘y’ coordinate. For e.g.

        js.executeScript(“window.scrollBy(0,450)”); //scrolling down
        Thread.sleep(2000);
        js.executeScript(“scroll(0, -250)”); //scrolling up

        Hope it helps!

        1. Yes It is helpfull with add this as well in Post.

          Thnks Nitin

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.