Ripon Al Wasim Ripon Al Wasim - 1 year ago 152
Java Question

Page scroll up or down in Selenium WebDriver (Selenium 2) using java

I have written the following code in Selenium 1 (a.k.a Selenium RC) for page scrolling using java:

selenium.getEval("scrollBy(0, 250)");

What is the equivalent code in Selenium 2 (WebDriver)?

Answer Source

For Scroll down:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");

OR, you can do as follows:

jse.executeScript("scroll(0, 250);");

For Scroll up:

jse.executeScript("window.scrollBy(0,-250)", "");
jse.executeScript("scroll(0, -250);");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download