Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to element with Selenium WebDriver

How do I get Selenium WebDriver to scroll to a particular element to get it on the screen. I have tried a lot of different options but have had no luck. Does this not work in the C# bindings?

I can make it jump to a particular location ex

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

But I want to be able to send it to different elements without giving the exact location each time.

public IWebElement Example { get { return Driver.FindElement(By.Id("123456")); } } 

Ex 1)

((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].scrollIntoView(true);", Example); 

Ex 2)

((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollBy(Example.Location.X", "Example.Location.Y - 100)"); 

When I watch it, it does not jump down the page to the element, and the exception matches the element being off screen.

I added an bool ex = Example.Exists(); after it and checked the results. It does Exist (its true). Its not Displayed (as its still offscreen as it has not moved to the element) Its not Selected ??????

Someone is seeing success By.ClassName. Does anyone know if there is a problem with doing this By.Id in the C# bindings?

like image 706
merrua Avatar asked Feb 12 '15 09:02

merrua


People also ask

How do you scroll with elements?

scroll() The scroll() method of the Element interface scrolls the element to a particular set of coordinates inside a given element.

How do you scroll a page in Selenium WebDriver?

New Selenium IDE Selenium can execute JavaScript commands with the help of the executeScript method. To scroll down vertically in a page we have to use the JavaScript command window. scrollBy. Then pass the pixel values to be traversed along the x and y axis for horizontal and vertical scroll respectively.

How do I scroll to the middle of the page in Selenium?

Selenium can execute JavaScript commands with the help of the executeScript method. To scroll to the middle of the screen, we have to first identify the element upto which we want to scroll on the page. Then pass scrollIntoView and the web element as parameters to the executeScript method.

How do I scroll to the right in Selenium?

scrollTo(document. body. scrollHeight,0)”); If you want to scroll horizontally in the right direction, use the following JavaScript.


2 Answers

Its little older question, but I believe that there is better solution than suggested above.

Here is original answer: https://stackoverflow.com/a/26461431/1221512

You should use Actions class to perform scrolling to element.

var element = driver.FindElement(By.id("element-id")); Actions actions = new Actions(driver); actions.MoveToElement(element); actions.Perform(); 
like image 116
DRAX Avatar answered Sep 20 '22 15:09

DRAX


This works for me in Chrome, IE8 & IE11:

public void ScrollTo(int xPosition = 0, int yPosition = 0) {     var js = String.Format("window.scrollTo({0}, {1})", xPosition, yPosition);     JavaScriptExecutor.ExecuteScript(js); }  public IWebElement ScrollToView(By selector) {     var element = WebDriver.FindElement(selector);     ScrollToView(element);     return element; }  public void ScrollToView(IWebElement element) {     if (element.Location.Y > 200)     {         ScrollTo(0, element.Location.Y - 100); // Make sure element is in the view but below the top navigation pane     }  } 
like image 28
milanio Avatar answered Sep 23 '22 15:09

milanio