Muna
Muna

Reputation: 453

Vertical Scroll down and scroll up in Selenium WebDriver with java

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.

Upvotes: 5

Views: 61380

Answers (4)

Caren
Caren

Reputation: 1418

Scrolling up should be as below:

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

Upvotes: 2

Aerien
Aerien

Reputation: 121

Scrolling to an Element of a page:

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();"
                                                              ,webElement);

Upvotes: 12

taha mokfi
taha mokfi

Reputation: 323

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.

Upvotes: 0

Ripon Al Wasim
Ripon Al Wasim

Reputation: 37756

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)", "");

Upvotes: 11

Related Questions