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...
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.
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)");
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().
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.
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!
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)", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With