Vertical Scroll down and scroll up in Selenium Web

2019-01-18 00:19发布

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条回答
孤傲高冷的网名
2楼-- · 2019-01-18 01:02

Scrolling to an Element of a page:

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();"
                                                              ,webElement);
查看更多
聊天终结者
3楼-- · 2019-01-18 01:02

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.

查看更多
贼婆χ
4楼-- · 2019-01-18 01:04

Scrolling up should be as below:

((JavascriptExecutor) driver).executeScript("scroll(0,-250);");
查看更多
Lonely孤独者°
5楼-- · 2019-01-18 01:16

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)", "");
查看更多
登录 后发表回答