瓦蒂尔.滚动到页面的某个点
我正在尝试在网站上自动进行在线调查,但每次都会出现此错误:
I am trying to automate an online survey on a website but I get this error each time:
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 "
我需要了解的是如何滚动到页面的某个点,以便我的脚本可以继续填写页面上的调查.
What I need to understand is how I can scroll to a certain point of the page so that my script can resume filling out the survey on the page.
这是我的代码,它设法填写了调查的一部分,但当它到达浏览器中未显示的行(需要用户向下滚动到的行)时失败:
This is my code that manages to fill out a portion of the survey but fails when it reaches a row which is not in view inside the browser (a row that requires the user to scroll down to):
buttons = browser.elements(:class => "assessment-choice")
buttons.each do |button|
button.click
end
我还希望能够更改我的代码,使其仅选择特定选项,但页面上的 HTML 不是很友好.
I would also like to be able to change my code so that it only selects a specific option but the HTML on the page is not very friendly.
这是我正在查看的网页:https://staging2.clearfit.com/assessment/assessment/95867fb272df436352a0bd5fbdd
This is the webpage I am looking at: https://staging2.clearfit.com/assessment/assessment/95867fb272df436352a0bd5fbdd
调查中选项之一的 HTML:
The HTML of one of the options on the survey:
<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>
使用execute_script
要滚动到一个元素,您需要执行 javascript:
To scroll to an element, you will need to execute javascript:
browser.execute_script('arguments[0].scrollIntoView();', button)
这可以在以下脚本中看到.如果没有要滚动的行,聊天选项卡会覆盖导致异常的按钮之一.
This can be seen to be working in the following script. Without the line to scroll, a chat tab overlays one of the buttons causing an exception.
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 允许该行简单地为:
Note that you can install the watir-scroll gem to make the scrolling line nicer. The gem allows the line to simply be:
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