Vertical Scroll down and scroll up in Selenium Web

2019-01-18 00:57发布

问题:

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.

回答1:

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:

Scrolling to an Element of a page:

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


回答3:

Scrolling up should be as below:

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


回答4:

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.