Monday, May 12, 2014

How to scroll down and up in any web page.

Note- window.scrollBy(forUp,forDown). Here in this method forUp, you can give any value depends on how much you want to scroll up but before that you have to scroll down so that there would be some space to scroll up.
Ex-

import java.util.concurrent.TimeUnit;

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

public class ScrollDown {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.flipkart.com/womens-clothing/pr?sid=2oq,c1r&otracker=hp_nmenu_sub_women_1_View%20all");
        driver.manage().window().maximize();
        JavascriptExecutor jsx = (JavascriptExecutor)driver;
        jsx.executeScript("window.scrollBy(0,4500)", "");
        Thread.sleep(3000);
        jsx.executeScript("window.scrollBy(2000,0)", "");
    }
}

4 comments:

  1. Hi dude, I have tried this example for some other URL. For me scroll up function not working, why?

    ReplyDelete
  2. Please give me your code, may be you have missed something..!!

    ReplyDelete
  3. Can you please check the below snippet dude....

    public class ScrollDown {

    public static void main(String[] args) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://selenium-makeiteasy.blogspot.in/2014/05/how-to-scroll-down-and-up-in-any-web.html#comment-form");
    driver.manage().window().maximize();
    JavascriptExecutor jsx = (JavascriptExecutor)driver;
    jsx.executeScript("window.scrollBy(0,4500)", "");
    Thread.sleep(3000);
    jsx.executeScript("window.scrollBy(1000,0)", "");
    }

    }

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete