I need scrool page in Selenium almost to the end of page (150 px until bottom). But my code doesn't work. It's scroll to bottom. How it's fix?
IWebElement element = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("javascript:window.scrollBy(0,document.body.scrollHeight-150)");
Try this:
((IJavaScriptExecutor)driver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 150)");
A few notes:
IWebElement
, so there is no need to have the IWebElement element =
part.javascript:
part eitherYou can scroll to the necessary location using javascript's scrollTo method.
public void scrollToElement(By by) {
Locatable element = (Locatable) selenium.findElement(by);
Point p= element.getCoordinates().getLocationOnScreen();
JavascriptExecutor js = (JavascriptExecutor) selenium;
js.executeScript("window.scrollTo(" + p.getX() + "," + (p.getY()) + ");");
}
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