Scroll to the end in Selenium

I need a scrool page in Selenium almost to the end of the page (150 pixels to the bottom). But my code is not working. He scrolls down. How to fix it?

IWebElement element = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("javascript:window.scrollBy(0,document.body.scrollHeight-150)"); 
+4
source share
2 answers

Try the following:

 ((IJavaScriptExecutor)driver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 150)"); 

A few notes:

  • A scrolling command is executed that does not return an IWebElement , so there is no need to have the IWebElement element = .
  • You don't need the javascript: part javascript: either
  • Since you want to scroll to an absolute position, scrollTo is better suited
+12
source

You can scroll to the desired location using the scrollTo javascript 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()) + ");"); } 
+1
source

Source: https://habr.com/ru/post/1502458/


All Articles