簡體   English   中英

瓦蒂爾。 滾動到頁面的某個點

[英]Watir. Scroll to a certain point of the page

我正在嘗試在網站上自動進行在線調查,但每次都會出現此錯誤:

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>

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

首先,這應該是不必要的。 根據規范,所有元素交互都需要隱式滾動到元素。 但是,如果確實阻止了這種情況的發生,您可以使用此 Selenium 方法而不是 javascript 實現:

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

然后你只需調用jump_to(element)並且它“跳轉”到它。

這就是我繞過它的方式 - 不確定這是否是正常方式。 問題是它到達點 (0,0); 處理移動到中心屏幕的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM