Skip to content

Instantly share code, notes, and snippets.

@dejanvasic85
Last active September 25, 2016 13:04
Show Gist options
  • Save dejanvasic85/3937d5ddd7f014f7f7026bdaeec24bb6 to your computer and use it in GitHub Desktop.
Save dejanvasic85/3937d5ddd7f014f7f7026bdaeec24bb6 to your computer and use it in GitHub Desktop.
Selenium WebElement scrolling so element is in the middle of page
public class LoginPage{
private IWebDriver _webDriver;
public LoginPage(IWebDriver webDriver){
_webDriver = webDriver;
}
[FindsBy(How = How.Name, Using = "username")]
private IWebElement UsernameInputElement {get;set;}
public void Login(string username){
_webDriver.ScrollElementToMiddle(UsernameInputElement).SendKeys(username);
}
}
public static IWebElement ScrollElementToMiddle(this IWebDriver driver, IWebElement element)
{
driver.ExecuteJavaScript("window.scrollTo(0, $(arguments[0]).offset().top - (window.innerHeight / 2));", element);
return element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment