Last active
September 25, 2016 13:04
-
-
Save dejanvasic85/3937d5ddd7f014f7f7026bdaeec24bb6 to your computer and use it in GitHub Desktop.
Selenium WebElement scrolling so element is in the middle of page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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