的Watir。滚动到页面的某个点

时间:2013-11-18 21:09:45

标签: ruby watir watir-webdriver ruby-1.9.3

我正在尝试在网站上自动进行在线调查,但每次都会收到此错误:

Selenium::WebDriver::Error::UnknownError: unknown error: Element is not clickable at  
point (561, 864). Other element would receive the click: a id="habla_oplink_a"       

class="habla_oplink_a_normal hbl_pal_header_font_size hbl_pal_title_fg "

我需要了解的是如何滚动到页面的某个位置,以便我的脚本可以继续填写页面上的调查。

这是我的代码,它设法填写调查的一部分,但是当它到达浏览器内部不在视图中的行(需要用户向下滚动到的行)时失败:

buttons = browser.elements(:class => "assessment-choice")

buttons.each do |button|
  button.click
end

我还希望能够更改我的代码,以便它只选择一个特定的选项,但页面上的HTML不是很友好。

这是我正在查看的网页:https://staging2.clearfit.com/assessment/assessment/95867fb272df436352a0bd5fbdd

调查中其中一个选项的HTML:

<a id="answers_79_0" class="assessment-choice" onmouseover="answerOver(this)"    onmouseout="answerOut(this)" onclick="setAssessmentAnswer(this, 3, '0', '79',   '#answers_49839163')">Strongly<br>Agree</a>

3 个答案:

答案 0 :(得分:8)

使用execute_script

要滚动到某个元素,您需要执行javascript:

browser.execute_script('arguments[0].scrollIntoView();', button)

可以看出这可以在以下脚本中使用。如果没有要滚动的行,聊天标签会覆盖其中一个按钮,从而导致异常。

require 'watir-webdriver'

browser = Watir::Browser.new :chrome
browser.goto 'https://staging2.clearfit.com/assessment/assessment/95867fb272df436352a0bd5fbdd'

buttons = browser.elements(:class => "assessment-choice")

buttons.each do |button|
    browser.execute_script('arguments[0].scrollIntoView();', button)
    button.click
end

使用watir-scroll gem

请注意,您可以安装watir-scroll gem以使滚动线更好。宝石允许该行简单地:

browser.scroll.to button

该脚本将如下所示:

require 'watir-webdriver'
require 'watir-scroll'

browser = Watir::Browser.new :chrome
browser.goto 'https://staging2.clearfit.com/assessment/assessment/95867fb272df436352a0bd5fbdd'

buttons = browser.elements(:class => "assessment-choice")

buttons.each do |button|
    browser.scroll.to button
    button.click
end

答案 1 :(得分:2)

首先,这应该是不必要的。 According to the spec,所有元素交互都需要隐式滚动到元素。但是,如果某些事情确实阻止了这种情况发生,您可以使用this Selenium method而不是javascript实现:

buttons = browser.elements(:class => "assessment-choice")

buttons.each do |button|
  button.wd.location_once_scrolled_into_view
  button.click
end

答案 2 :(得分:1)

  public

  def scroll_to(param)
    args = case param
           when :top, :start
             'window.scrollTo(0, 0);'
           when :center
             'window.scrollTo(window.outerWidth / 2, window.outerHeight / 2);'
           when :bottom, :end
             'window.scrollTo(0, document.body.scrollHeight);'
           when Array
             ['window.scrollTo(arguments[0], arguments[1]);', Integer(param[0]), Integer(param[1])]
           else
             raise ArgumentError, "Don't know how to scroll to: #{param}!"
           end

    @browser.execute_script(*args)
  end
  public

  # This method pulls the object on the page you want to interact with, then it 'jumps to it'.
  def jump_to(param)
    # Leveraging the scroll_to(param) logic, this grabs the cooridnates,
    # and then makes them an array that is able to be located and moved to.
    # This is helpful when pages are getting too long and you need to click a button
    # or interact with the browser, but the page 'Cannot locate element'.
    location = param.wd.location
    location = location.to_a
    $helper.scroll_to(location)
  end

然后你只需拨打jump_to(element)即可#34;跳转&#34;它。

这就是我如何解决它 - 不确定这是否是正常的方式。问题是它是指向(0,0);处理移动到中心屏幕的版本。