[フレーム]

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

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

Reader Interactions

Comments

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

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

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

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

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

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

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

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

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

      • 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!

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.

AltStyle によって変換されたページ (->オリジナル) /