我是靠谱客的博主 义气小蝴蝶,这篇文章主要介绍Selenium如何滚动到页面底部,现在分享给大家,希望可以做个参考。

底下这段代码演示了如何滚动到页面底部。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<span style="font-size:14px;">public void scrollToBottom() { for (int i = 10; i > 0; i--) { //这里列出所有相似的元素,这类元素一直会显示到页面底部,比如grid,比如一些blog的标题什么的 List<WebElement> summarytext = driver.findElements(By.className("FeedControl")); //这里得到页面最后一个元素 WebElement lastNote = summarytext.get(summarytext.size() - 1); //定义一个Coordinates的一个类型,将页面最后一个元素赋值给变量coordinate Coordinates coordinate = ((Locatable) lastNote).getCoordinates(); //onPage()这个方法得到这个元素在整个页面上靠左上角的一个坐标 coordinate.onPage(); //inViewPort()这个方法可以自动滚动到坐标位置,使得此元素在页面可见。 coordinate.inViewPort(); WaitTool.ajaxWait(); } }</span>


底下是Coordinates.class的解释,可以加深了解。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<span style="font-size:14px;">package org.openqa.selenium.interactions.internal; import org.openqa.selenium.Point; /** * Provides coordinates of an element for advanced interactions. Note that some coordinates (such as * screen coordinates) are evaluated lazily since the element may have to be scrolled into view. */ public interface Coordinates { /** * Gets coordinates on the element relative to the top-left corner of the monitor (screen). * This method automatically scrolls the page and/or frames to make element visible in viewport * before calculating its coordinates. * * @return coordinates on the element relative to the top-left corner of the monitor (screen). * @throws org.openqa.selenium.ElementNotVisibleException if the element can't be scrolled into view. */ Point onScreen(); /** * Gets coordinates on the element relative to the top-left corner of OS-window being used * to display the content. Usually it is the browser window's viewport. This method automatically * scrolls the page and/or frames to make element visible in viewport before calculating its coordinates. * * @return coordinates on the element relative to the top-left corner of the browser window's viewport. * @throws org.openqa.selenium.ElementNotVisibleException if the element can't be scrolled into view. */ Point inViewPort(); /** * Gets coordinates on the element relative to the top-left corner of the page. * * @return coordinates on the element relative to the top-left corner of the the page. */ Point onPage(); Object getAuxiliary(); }</span>



最后

以上就是义气小蝴蝶最近收集整理的关于Selenium如何滚动到页面底部的全部内容,更多相关Selenium如何滚动到页面底部内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部