概述
底下这段代码演示了如何滚动到页面底部。
<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的解释,可以加深了解。
<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如何滚动到页面底部所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复