Reputation: 168
I am trying to scroll my window up in automated testing environment, but this just does not work (also no errors in console):
public void ScrollToTheTop()
{
if (this.driver is RemoteWebDriver remoteDriver)
{
IJavaScriptExecutor jex = remoteDriver;
jex.ExecuteScript("window.scrollTo(0,0)");
}
}
Now if I try to execute jex.ExecuteScript("alert('Something')")
it works fine, which means that js gets executed correctly. But for some reason scroll operations do not work (tried scrollBy()
aswell).
Does anybody know how to execute scroll scripts? Thank you.
Upvotes: 2
Views: 1478
Reputation: 29362
If you want to scroll in C#, Using ExecuteScript please use below code :
((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollTo(0, Y)");
Where Y
should be vertical axis.
to scroll down :
((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollTo(0, 200)");
or to scroll up :
((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollTo(0, -200)");
To move to a specific web element in C#-Selenium bindings :
IWebElement e = Driver.FindElement(By.XPath(xpath of the web element));
((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].scrollIntoView(true);", e);
to move till end of the page :
((IJavaScriptExecutor)Driver).ExecuteScript("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;");
Upvotes: 1