7

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...

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Keerthana
  • 71
  • 1
  • 1
  • 2
  • 1
    possible duplicate of [Page scroll up or down in Selenium WebDriver (Selenium 2) using java](http://stackoverflow.com/questions/12293158/page-scroll-up-or-down-in-selenium-webdriver-selenium-2-using-java) – Vignesh Paramasivam Aug 18 '14 at 13:19
  • Try my solution https://stackoverflow.com/a/66774605/2202107 – Sida Zhou Mar 24 '21 at 04:05

3 Answers3

3

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!

Sitam Jana
  • 3,123
  • 2
  • 21
  • 40
3

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)", "");
Z4-tier
  • 7,287
  • 3
  • 26
  • 42
1

Another thing that worked for me was the browser accessibility and TAB button. As you know by sending TAB you can navigate trough the page as far as you know how many times you need to do it to get where you want. Also you can try clicking Arrows Up/Down:

 1.Element.SendKeys(OpenQA.Selenium.Keys.ArrowUp);
 2. char u = '\uE013'; // ASCII code for ArrowUp
    char d = '\u0x50'; // code for ArrowDown
    Element.SendKeys(Convert.ToString(u));
ekostadinov
  • 6,880
  • 3
  • 29
  • 47