Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse scroll down using Selenium WebDriver (a.k.a. Selenium 2.0) - JAVA

I am trying to do a mouse scroll in my automation testing (selenium webdriver). My page have loads of data where it take time to load all the datum.

My Requirement: I have a consolidated table with set of datum, where those records are displayed from the set of values that get displayed in the bottom of my page.

I am validating whether both the values are equal, for that I need the page to be completely scrolled to evaluate the same.

I used the below code:

Javascript jse = (Javascript)driver;
jse.executescript("scroll(0, 9000)");

This doesn't help it scrolled only half of the datum so my test get fail.

Suggestions please...

like image 289
Keerthana Avatar asked Aug 18 '14 12:08

Keerthana


People also ask

How do I scroll down and scroll in Selenium?

Hence, to scroll up or down with Selenium, a JavaScriptExecutor is a must. The scrollBy() method involves two parameters, x, and y, that represent the horizontal and vertical pixel values, respectively.

How do I scroll using Selenium?

Javascript method ScrollBy() scrolls the web page to the specific number of pixels. The syntax of ScrollBy() methods is : executeScript("window. scrollBy(x-pixels,y-pixels)");

How do you scroll down to the bottom of a page in Selenium?

Selenium runs the commands in Javascript with the execute_script() method. For scrolling down to the bottom of the page, we have to pass (0, document. body. scrollHeight) as parameters to the method scrollBy().

Why scroll is not working in Selenium?

Selenium cannot directly scroll up or down with its methods. This is done with the Javascript Executor. DOM can interact with the elements with Javascript. Selenium runs the commands in Javascript with the execute_script() method.


2 Answers

We can use JavascriptExecutor to achieve this. Following is an example which will scroll this page from top to bottom:

WebDriver driver = new ChromeDriver();
driver.get("http://stackoverflow.com/questions/25363023/mouse-scroll-down-using-selenium-webdriver-2-0-java");

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollTo(0,document.body.scrollHeight);");

To use above code please import below utilities:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

Lemme know if this helps!

like image 55
Sitam Jana Avatar answered Oct 27 '22 00:10

Sitam Jana


Use these imports:

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

Mouse Scroll Down:

JavascriptExecutor Scrool = (JavascriptExecutor) driver;
Scrool.executeScript("window.scrollBy(0,300)", "");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

You can find elements after Scrolling:

driver.findElement(By.xpath(""));

Mouse Scroll up:

JavascriptExecutor Scrool = (JavascriptExecutor) driver;
Scrool.executeScript("window.scrollBy(0,-300)", "");
like image 33
Dinesh Reddy Avatar answered Oct 27 '22 01:10

Dinesh Reddy