11/25/17

Scroll Web Page Down Or UP Using WebDriver


There are many ways to Scroll Web Page DOWN or UP.
JavaScript scrollBy() method scrolls the document by the specified number of pixels.

Syntax:

window.scrollBy(xnum, ynum)
Parameters:
  • xnum is a Number
  • Required. How many pixels to scroll by, along the x-axis (horizontal). Positive values will scroll to the right, while negative values will scroll to the left
  • ynum is a Number
  • Required. How many pixels to scroll by, along the y-axis (vertical). Positive values will scroll down, while negative values scroll up
Return Value:
No return value

To Scroll Web page Down using WebDriver:

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

public class HandleScroll {

public static void main(String args[]) {
System.setProperty("webdriver.gecko.driver", "C://Drivers//geckodriver-v0.19.1-win64//geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.pavantestingtools.com");

// to perform Scroll on application using Selenium
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)", "");
}
}

To scroll to the Bottom of the Web Page using WebDriver:

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

public class HandleScroll {

public static void main(String args[]) {
System.setProperty("webdriver.gecko.driver", "C://Drivers//geckodriver-v0.19.1-win64//geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.pavantestingtools.com");

// to perform Scroll on application using Selenium
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
}
}

To Scroll Web page Up using WebDriver:

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

public class HandleScroll {

public static void main(String args[]) {
System.setProperty("webdriver.gecko.driver", "C://Drivers//geckodriver-v0.19.1-win64//geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.pavantestingtools.com");

// to perform Scroll on application using Selenium
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,-500)", "");
}
}

0 comments:

Post a Comment

Google Q&A Forum