How to scroll web page Up or Down using Selenium Webdriver?

In this post we will learn How to scroll web page Up or Down using Selenium Webdriver. There are various methods for scrolling page vertically and horizontally both. Go through to the below description, code and execution as well which will let you know to work on scroll conditions of a web page.

How to scroll web page Up or Down using Selenium Webdriver?

Scroller is mostly automated while testing E-commerce or any social media application. Let’s take an example of very popular website www.amazon.in Before automating amazon find the description of methods used in selenium webdriver to scroll page.

Before starting you have to import jar files in eclipse. CLICK HERE to download desired Jar’s and import the same in your system.

How to scroll page Down Using Selenium Webdriver:

Window.scrollBy() method is used to scroll down the page by specified number of pixels.

Syntax:
window.scrollBy(xpixels,ypixels)

xpixels:

  1. How many pixels to scroll by X-Axis, in other words we can say scroll page horizontally.
  2. Positive values will scroll the page to the left and negative values will scroll the page to the right.

ypixels:

  • How many pixels to be scrolled along y axis or to scroll the page vertically.

Positive values will scroll down while negative values will scroll Up.

1. If you want to scroll page in vertically down, you can use following code:

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript(“window.scrollTo(0,document.body.scrollHeight)”);

2. If you want to scroll page at the particular element, you can use following code:

WebElement element = driver.findElement(By.id(“element id”));

((JavascriptExecutor)driver).executeScript(“srguments[0].scrollintoView();”, element);

— First create an object with the name of element.
— Give reference of particular element in second line.

3. If you want to scroll page at the particular coordinate, you can use following code:

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript(“window.scrollBy(200,250)”);

Mention the coordinated in brackets (200,250) page will scroll to the particular coordinate.


How to scroll page Down Using Selenium Webdriver:

Values methods to scroll up the page:

1. If you want to scroll page horizontally in right, Use the following code for the same:

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript(“window.scrollBy(250,0)”);

2. If you want to scroll page horizontally in left, use negative coordinated to achieve it. Use the following code for the same:

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript(“window.scrollBy(0,-2000)”);

CODE:
Refer the below code for clarification:

package system.admin;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Pagescroll {public static void main(String[] args) throws InterruptedException

{

WebDriver driver = new FirefoxDriver();

driver.navigate().to(“http://www.amazon.in/”);

driver.findElement(By.id(“twotabsearchtextbox”)).sendKeys(“mens clothing”);

driver.findElement(By.xpath(“/html/body/div[1]/header/div/div[1]/div[3]/div/form/div[2]/div/input”)).click();

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript(“window.scrollTo(0,document.body.scrollHeight)”);

}

}

Steps to execute the above code:

1. Right click on class.
2. Click on Run As -> Java Application.

Code will execute as follows:

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *