Selenium如何滚动到页面底部

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

<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>




版权声明:本文为anly8828原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。