• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to Scroll Page in Selenium Webdriver using Java Script

How to Scroll Page in Selenium Webdriver using Java Script

October 15, 2016 by Mukesh Otwani 34 Comments

Scroll page in Selenium Webdriver

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.

Filed Under: Advance Selenium Tagged With: Page Scroll in Selenium

Reader Interactions

Comments

  1. Vetrivelan says

    January 24, 2017 at 10:29 PM

    Great article Mukesh. Thanks for sharing the same.

    Reply
    • Mukesh Otwani says

      January 25, 2017 at 4:59 PM

      Hi Vetrivelan,

      Very very thanks for your comments.

      Reply
  2. sreedhar says

    January 17, 2017 at 1:00 PM

    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.

    Reply
    • Mukesh Otwani says

      January 19, 2017 at 12:30 PM

      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 http://learn-automation.com/how-to-scroll-into-view-in-selenium-webdriver/

      Reply
  3. Siddharth says

    January 2, 2017 at 3:27 PM

    Hi Mukesh

    Thanks for your support and wish you happy New Year

    Reply
  4. NITESH JAIN says

    December 22, 2016 at 11:00 AM

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

    Reply
    • Mukesh Otwani says

      January 11, 2017 at 3:24 PM

      Thanks Nitesh

      Reply
  5. Chetan Yeshi says

    November 25, 2016 at 9:59 AM

    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;
    }

    Reply
    • Mukesh Otwani says

      November 25, 2016 at 2:59 PM

      Hey Chetan,

      Thanks for the new solution.

      Reply
  6. HimaBindu says

    October 22, 2016 at 4:58 AM

    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);

    Reply
    • Mukesh Otwani says

      October 25, 2016 at 2:08 PM

      I use scroll method to scroll but you have used scrollBy

      Reply
  7. Gaurav Khurana says

    October 19, 2016 at 11:43 PM

    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

    Reply
    • Mukesh Otwani says

      October 20, 2016 at 1:49 PM

      Good question let me check.

      Reply
  8. rakesh says

    August 22, 2016 at 3:56 PM

    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.

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:23 PM

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

      Reply
  9. Aswini says

    August 1, 2016 at 3:02 PM

    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

    Reply
    • Mukesh Otwani says

      August 2, 2016 at 11:54 AM

      Hi Aswini,

      Can u try below code

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

      Reply
      • Ramya says

        September 30, 2016 at 1:42 PM

        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();

        Reply
        • Mukesh Otwani says

          October 3, 2016 at 4:37 PM

          Thanks Ramya for update.

          Reply
  10. Nivi says

    July 18, 2016 at 1:19 PM

    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);

    Reply
    • Mukesh Otwani says

      July 20, 2016 at 2:00 PM

      For mobile use below link

      http://learn-automation.com/scroll-in-appium-for-android-and-ios/

      Reply
      • Nivi says

        July 22, 2016 at 9:53 AM

        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 ?

        Reply
        • Mukesh Otwani says

          July 25, 2016 at 12:17 PM

          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

          Reply
  11. Sandip says

    June 26, 2016 at 11:44 AM

    I really appreciate this Selenium Stuff its so heal full…

    Reply
    • Mukesh Otwani says

      July 2, 2016 at 3:12 PM

      Thanks Mate 🙂

      Reply
  12. bansalvarsha says

    March 31, 2016 at 4:37 PM

    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

    Reply
    • Mukesh Otwani says

      April 1, 2016 at 5:03 PM

      Hi Varsha,

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

      http://learn-automation.com/how-to-scroll-into-view-in-selenium-webdriver/

      Reply
  13. Nitin says

    March 13, 2016 at 4:49 PM

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

    Reply
  14. sarojsingh22011 says

    January 12, 2016 at 3:48 PM

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

    Reply
    • Mukesh Otwani says

      January 12, 2016 at 6:36 PM

      Thanks Saroj 🙂 Keep visiting.

      Reply
  15. Puneeth says

    April 1, 2015 at 2:13 PM

    Simple and very easy one.

    Reply
    • aaa says

      November 27, 2015 at 4:46 PM

      Hi Mukesh,

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

      Reply
      • Nitin says

        April 7, 2016 at 10:59 PM

        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!

        Reply
        • Mukesh Otwani says

          April 10, 2016 at 11:40 AM

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

          Thnks Nitin

          Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?