How to scroll to element with Selenium WebDriver

105,664

Solution 1

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();

Solution 2

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
    }

}

Solution 3

This works for me:

var elem = driver.FindElement(By.ClassName("something"));
driver.ExecuteScript("arguments[0].scrollIntoView(true);", elem);

Solution 4

I created a extension for IWebDriver:

public static IWebElement GetElementAndScrollTo(this IWebDriver driver, By by)
{
    var js = (IJavaScriptExecutor)driver;
    try
    {
        var element = driver.FindElement(by);
        if (element.Location.Y > 200)
        {
            js.ExecuteScript($"window.scrollTo({0}, {element.Location.Y - 200 })");
        }
        return element;
    }
    catch (Exception ex)
    {
        return null;
    }
}

Solution 5

This works for me in C# automation:

public Page scrollUp()
{
    IWebElement s = driver.FindElement(By.Id("your_locator")); ;
    IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
    je.ExecuteScript("arguments[0].scrollIntoView(false);", s);
    return this;
}
Share:
105,664
merrua
Author by

merrua

I will read more programming books this year.

Updated on September 29, 2021

Comments

  • merrua
    merrua over 2 years

    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?