Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Last active April 5, 2021 15:52
Embed
What would you like to do?
How to scroll an element into a view using Selenium c#
element = driver.FindElement(By.LinkText("Click me using this link text!"));
//this will scroll the element and center it for interaction
var js = (IJavaScriptExecutor)Driver;
js.ExecuteScript("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'})", element);
//this one will scroll the element into view for interactions
IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
je.ExecuteScript("arguments[0].scrollIntoView(false);", element);
@ccasalicchio
Copy link

For C# try this

        public void ScrollToId(string id)
        {
            this.Driver.ExecuteJavascript($"document.getElementById('{id}').scrollIntoView({{behavior: 'smooth', block: 'center'}});");
        }
        public static object ExecuteJavascript(this IWebDriver driver, string script)
        {
            return ((IJavaScriptExecutor)driver).ExecuteScript(script);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment