如何使用watir滚动网页 [英] how to scroll a web page using watir
问题描述
我想滚动网页,以查找并点击在滚动页面时懒散加载的内容。我使用以下命令
I am trying to scroll a web page to find and click on a content that is lazily loaded when the page is scrolled. I am using following command
require 'watir-webdriver'
@browser = Watir::new :firefox
@browser.send_keys :space
我使用web-driver with firefox我在Ubuntu,但它不工作。在下面的ruby代码中,我试图向下滚动页面,直到我找不到与:id的元素。元素正在加载。
我在几秒钟后超时,任何想法下面的代码是什么问题。
I am using web-driver with firefox and I am on ubuntu but it is not working. In the following ruby code I am trying to scroll the page down until I don't find the element with :id. The element is loading lazily. I am getting timeout after few seconds, any idea what is wrong with the following code.
When /^deal (\d+) is loaded$/ do |id|
(0..5).each do |click|
@browser.send_keys :space
end
end
推荐答案
如果您启用了JavaScript,您可以访问底层驱动程序并执行一些JavaScript在页面上滚动。
If you have JavaScript enabled, you can access the underlying driver and execute some JavaScript to scroll on the page.
@browser.driver.executeScript("window.scrollBy(0,200)")
将沿着y轴向下滚动200像素。
will scroll down the page 200 pixels along the y axix.
有关方法的文档,请参阅此处:
See here for documentation of the method:
http://www.w3schools.com/jsref/met_win_scrollby.asp
这篇关于如何使用watir滚动网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!