【问题标题】:Watir. Scroll to a certain point of the page瓦提尔。滚动到页面的某个点
【发布时间】:2013-12-02 04:52:53
【问题描述】:

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

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>

【问题讨论】:

  • 我没有看到那个错误。您能否提供一个完整的脚本来重现问题(即更清楚地说明异常发生的位置)?
  • 提供更多细节。
  • 我的代码是输入到 irb 中的,所以目前我还没有完整的脚本。如果我要填写整个调查,我必须向下滚动,因为我的屏幕不适合整个页面。抱歉,我有点不清楚我还能提供什么。
  • 由于您的页面是公开的,应该可以创建一个独立的脚本,我们可以运行它来查看您看到的确切错误(即,如果我们复制代码然后运行它,我们应该看到同样的错误)。到目前为止,我们无法使用所提供的信息重现您的问题。我的部分困惑是你说提供的按钮单击脚本有效......但没有给出其他代码,那么实际上哪些代码失败了?
  • 好的,我知道为什么我无法重现该问题。使用 Firefox 时不会出现,但使用 Chrome 时可以看到。

标签: ruby watir watir-webdriver ruby-1.9.3


【解决方案1】:

使用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 以使滚动线更好。 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

【讨论】:

  • 它给出了错误undefined method scroll' for #&lt;Watir::Browser:0x314d858&gt; (NoMethodError)
  • 你真的安装了 watir-scroll gem 吗? browser.scroll 不是标准 Watir API 的一部分。
  • 是的,我确实安装了它。我将gemfile 更新为gem 'watir-scroll',然后bundle install。
  • 您是否也需要它 - 即require 'watir-scroll'?
  • browser.scroll.to button 对我不起作用,但 button.scroll.to 起作用。这是 gem README 使用的格式
【解决方案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

【讨论】:

    【解决方案3】:
      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),它就会“跳转”到它。

    这就是我解决它的方法-不确定这是否是正常的方法。问题是它指向(0,0)点;正在开发一个移动到中心屏幕的版本。

    【讨论】:

      猜你喜欢
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 2015-06-28
      • 2021-08-05
      • 2012-04-03
      • 1970-01-01
      相关资源
      最近更新 更多