JAVA Script Executor Code to Scroll to an Element

Scroll to an Element in Selenium WebDriver

Some times there is need to scroll to an element and perform some operations. We can use the JavascriptExecutor to scroll down to an element.

Below code is used to scroll down to an element. We have used the getLocation().y to get the y axis position of the element. In the same way we can use the x axis position of the element.


1
2
3
4
5
6
7
@FindBy(id = Checkbox.financialAssistanceAccountEmailId)
public WebElement financialAssistanceAccountEmail;

//Scroll to element

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0,"+financialAssistanceAccountEmail.getLocation().y+")");

Comments

Popular posts from this blog

SQL Day 11: Group By in SQL Server

JAVA Script Executor Code to Click on an Element

SQL Day 2: Creating, Altering and Dropping a Database