Ruby 瓦蒂尔。滚动到页面的某一点
我试图在网站上自动执行在线调查,但每次都会出现以下错误:Ruby 瓦蒂尔。滚动到页面的某一点,ruby,watir,watir-webdriver,ruby-1.9.3,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_fon
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 "
我需要了解的是如何滚动到页面的某个点,以便脚本可以继续在页面上填写调查
这是我的代码,它成功地填写了调查的一部分,但在到达浏览器中不可见的行(需要用户向下滚动到的行)时失败:
我也希望能够改变我的代码,使它只选择一个特定的选项,但网页上的HTML不是很友好
这是我正在查看的网页:
调查中一个选项的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>
强烈地
同意
使用执行脚本
要滚动到某个元素,您需要执行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
使用水卷轴宝石
请注意,您可以安装以使滚动线更漂亮。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
首先,这应该是不必要的,所有元素交互都需要隐式滚动到元素。但是,如果确实有什么东西阻止了这种情况的发生,您可以使用以下替代javascript实现:
buttons = browser.elements(:class => "assessment-choice")
buttons.each do |button|
button.wd.location_once_scrolled_into_view
button.click
end
然后您只需调用jump\u to(element)
,它就会“跳转”到它
我就是这样绕过它的——不确定这是否正常。问题是它指向(0,0)点;正在处理移动到中央屏幕的版本。我没有看到该错误。你能提供一个完整的脚本来重现这个问题吗(例如,让异常发生的地方更清楚)?提供更多的细节。我已经输入到irb的代码,所以我现在没有完整的脚本。如果我要填写整个调查,我必须向下滚动,因为我的屏幕不适合整个页面。抱歉,我有点不清楚我还能提供什么。因为您的页面是公开的,所以应该可以创建一个独立的脚本,我们可以运行它来查看您看到的确切错误(即如果我们复制代码,然后运行它,我们应该会看到相同的错误)。到目前为止,我们无法用提供的信息再现您的问题。我感到困惑的一部分是你说提供的点击按钮脚本可以工作。。。但是没有给出其他代码,那么什么代码实际上失败了?好吧,我找到了为什么我不能重现这个问题。它在使用Firefox时不会出现,但在使用Chrome时可以看到。它为#(NoMethodError)提供了错误undefined method scroll'
您是否实际安装了watir scroll gem<代码>浏览器。滚动
不是标准Watir API的一部分。是的,我安装了它。我用gem'watir scroll'
行更新了gem文件
,然后bundle安装
。您也需要它吗?ierequire'watir scroll'
?browser.scroll.to按钮
对我不起作用,但按钮.scroll.to
对我起作用。这就是gem自述文件使用的格式
buttons = browser.elements(:class => "assessment-choice")
buttons.each do |button|
button.wd.location_once_scrolled_into_view
button.click
end
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