[英]Selenium-webdriver/Javascript: How do I scroll through a page?
I'm using the Javascript API to selenium-webdriver and I would like to scroll/move down through a page slowly to allow for visual inspection. 我正在将Javascript API用于selenium-webdriver,我想缓慢滚动/向下滚动页面以进行视觉检查。
I know the following will jump me to a link at the end of my page: 我知道以下内容将使我跳到页面结尾的链接:
return driver.findElement({linkText: 'All rights reserved'}).click()
However, I'd like to review the stuff in between, as I watch selenium do its thing. 但是,我想回顾一下介于两者之间的内容,因为我看到了硒的作用。
Is it possible to scroll incrementally through a page? 是否可以逐步滚动浏览页面? (eg 10%, 20%, 30% etc.). (例如10%,20%,30%等)。 Or is there another technique to effectively do a "slow scroll"? 还是有另一种技术可以有效地进行“慢滚动”?
This can be achieved in two ways- 1.Scroll the page in slow motion- you can change time in Thread.sleep as per requirement. 这可以通过两种方式实现:1.以慢动作滚动页面-您可以根据需要在Thread.sleep中更改时间。
JavascriptExecutor js = (JavascriptExecutor)driver;
for (int i = 0;; i++)
{
if(i >=60)
{
break;
}
js.executeScript("window.scrollBy(0,800)", ""); //800 is Y coordinate. This can be changed
Thread.sleep(3000);
}
2.Scroll to element position before performing operation on it- 2.滚动到元素位置,然后对其执行操作-
Point p = driver.findElement(By.xpath("Value")).getLocation();//get element position
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+(p.getY())+");");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.