简体   繁体   English

如何使用 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

I am using web-driver with firefox and I am on ubuntu but it is not working.我在 firefox 上使用网络驱动程序,我在 ubuntu 上,但它不工作。 In the following ruby code I am trying to scroll the page down until I don't find the element with:id.在下面的 ruby 代码中,我试图向下滚动页面,直到找不到带有: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

What is the best way to scroll a page using watir-driver?使用 watir-driver 滚动页面的最佳方式是什么?

If you have JavaScript enabled, you can access the underlying driver and execute some JavaScript to scroll on the page.如果启用了 JavaScript,则可以访问底层驱动程序并执行一些 JavaScript 以在页面上滚动。

@browser.driver.executeScript("window.scrollBy(0,200)")

will scroll down the page 200 pixels along the y axix.将沿 y 轴向下滚动页面 200 像素。

See here for documentation of the method:有关该方法的文档,请参见此处:

http://www.w3schools.com/jsref/met_win_scrollby.asp http://www.w3schools.com/jsref/met_win_scrollby.asp

I use a gem called "watir-scroll" to assist me with this.我使用一个名为“watir-scroll”的 gem 来帮助我。 Though it usually needs places to scroll to, it also will scroll to coordinates.虽然它通常需要滚动到位置,但它也会滚动到坐标。

https://github.com/p0deje/watir-scroll Since Watir v6.16 watir-scroll gem merged into watir https://github.com/p0deje/watir-scroll自 Watir v6.16 watir-scroll gem 合并到watir

You can either scroll to a specific element您可以滚动到特定元素

button1 = @browser.input(:class => "mileage_rate")
@browser.scroll.to button1

Or just scroll to the top middle or center或者只是滚动到顶部中间或中心

@browser.scroll.to :top
@browser.scroll.to :center
@browser.scroll.to :bottom

Or scroll to a coordinate或者滚动到一个坐标

browser.scroll.to [0, 200]

This saved me a bunch of time:这节省了我很多时间:

browser.div(:id => 'start-date-holder').wd.location_once_scrolled_into_view 

Sorry I could not comment on the last answer since I am new here and do not have enough rep pts yet so I just created a new answer.抱歉,我无法对最后一个答案发表评论,因为我是新来的,而且还没有足够的代表,所以我刚刚创建了一个新答案。 Anyways, if anyone is having issues with scrolling multiple times try this (add a loop and sleep):不管怎样,如果有人在多次滚动时遇到问题,试试这个(添加一个循环并休眠):

maximum_times_needed = max # of times you need the page to scroll down

maximum_times_needed.each do
@browser.driver.executeScript("window.scrollBy(0,200)")
sleep 0.15
end

0.15 may vary depending on how long it takes the page to load. 0.15可能会有所不同,具体取决于页面加载所需的时间。 0.15 is 0.15 seconds so adjust as needed to allow for enough time for the page to load. 0.15是 0.15 秒,因此请根据需要进行调整,以便为页面加载留出足够的时间。 The 200 may also need to be adjusted to a larger pixel amount. 200可能还需要调整到更大的像素量。

    300.times do
      @browser.driver.execute_script("window.scrollBy(0,200)")
      sleep 0.05
    end

You can access the underlying driver and execute some javascript.您可以访问底层驱动程序并执行一些 javascript。 For instance if you wanted to scroll to the bottom of the page you would use例如,如果您想滚动到页面底部,您将使用

@browser.driver.execute_script( "window.scrollBy(0,document.body.scrollHeight)" )  

which scrolls to the bottom of the page on the y-axis.它在 y 轴上滚动到页面底部。

It works有用

evaluate_script("document.getElementsByTagName('body')[0].scrollTop=0;")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM