How To Scroll Web Page Down Or UP Using Selenium WebDriver
Let’s see how to Scroll Web Page DOWN or UP in this post. There are few ways to scroll Web page up or down using Selenum WebDriver. We are going to see those in this post in detail.
JavaScript scrollBy() method scrolls the document by the specified number of pixels.
Also Read: How To Use JavaScriptExecutor in Selenium
Syntax:
window.scrollBy(xnum,Â
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 Selenium WebDriver:
In the below example we use 250 pixels for vertical scrolling in order to see how scroll method works
package softwareTestingMaterial; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleScroll { @Test public void scrollDown(){ System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.navigate().to("https://www.softwaretestingmaterial.com"); //to perform Scroll on application using Selenium JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,250)", ""); } }
To scroll to the Bottom of the Web Page using Selenium WebDriver:
We get the height of the Body element from the DOM (Document Object Model) and we use the scrollTo() method to scroll to the maximum height of the page:
package softwareTestingMaterial; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleScroll { @Test public void scrollDown(){ System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.navigate().to("https://www.softwaretestingmaterial.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 Sleneium WebDriver:
package softwareTestingMaterial; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class HandleScroll { @Test public void scrollDown(){ System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.navigate().to("https://www.softwaretestingmaterial.com"); //to perform Scroll on application using Selenium JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,-250)", ""); } }
If you are not a regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
Very Thank you !! It is very helpful.But now my query is How can we scroll down webpage using web driver sampler in jmeter.