Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)
edited by

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.

Update: 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?

1 Answer

0 votes
by (62.9k points)

You can use Actions class to perform scrolling to web elements.

var element = driver.FindElement(By.id("element-id"));

Actions actions = new Actions(driver);

actions.MoveToElement(element);

actions.Perform();

Learn Selenium with the help of this Selenium Tutorial by Intellipaat.

To learn in-depth about Selenium, sign up for an industry based Selenium training.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...