In my environment using Selenium v4.12.1 with JDK 21, I could not get the page scrolling working using the JavascriptExecutor. What I tried are these various approaches...
/**
* scroll vertically(down) https://testsigma.com/blog/scrolling-in-selenium/
* @param driver
* @param ratio -- between 0.5-1.0
*/
public void scrollJSVertical(RemoteWebDriver driver,double ratio) {
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(String.format("window.scrollTo(0, document.body.scrollHeight * %.1f)",ratio));
}
/**
* Scroll by the pixel value on x, y coordinates
* @param driver
* @param x
* @param y
*/
public void scrollByJS(RemoteWebDriver driver,int x, int y) {
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,900)", "");
}
/**
* scroll based on the visibility of the web element on the page
* @param driver
* @param elm WebElement present on the DOM
*/
public void scrollJS4ElementVisibility(RemoteWebDriver driver,WebElement elm) {
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].scrollIntoView();", elm);
}
Upon building a demo project to test these methods, all of them worked just fine even including on the mobile device emulators. Only issue I am facing is they are not working for the Selenium script running on a cloud platform happened to be BrowserStack. I raised a support ticket with them and pursuing it.
Thanks,