5

Can anybody please help me out to automate scroll down functionality with WebDriver using Java?

In my case, For yahoo mail "Sign In" is getting displayed (visible) once I scroll down the mouse vertically.

4 Answers 4

12

Scrolling to an Element of a page:

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();"
                                                              ,webElement);
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, this works nicely. I've been trying this for hours and none of the other options worked
Great for focusing an element before taking a screenshot.
11

You can scroll down vertically by using the following code:

((JavascriptExecutor) driver).executeScript("scroll(0,250);");

Similarly, it is also possible to scroll up by changing y coordinate as negative:

((JavascriptExecutor) driver).executeScript("scroll(0, -250);");

You can also use the following code: For scroll down:

((JavascriptExecutor) driver).executeScript("window.scrollBy(0,250)", "");

For scroll up:

((JavascriptExecutor) driver).executeScript("window.scrollBy(0, -250)", "");

2 Comments

How to know when scrolling is completed?
To make the visibility of any element sometime scrolling is needed. For example, you are going to click an element which is located at the bottom of the page. In this case, it needs down scrolling. When click is succeeded, it means scrolling is done and link at the bottom is visible.
2

Scrolling up should be as below:

((JavascriptExecutor) driver).executeScript("scroll(0,-250);");

1 Comment

Caren is right. no need to change the x coordinate. y coordinate should be negative to scroll up.
0

If you are not sure about the height of the page and you are gonna scroll down to the down part of the page you can find the main frame of that page and use following code to scroll down without using scroll or scrollBy

scr1 = driver.find_element_by_xpath('xpath')
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", scr1)

This will automatically go to the far down of the page. You can see an example here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.