Watir. Scroll to a specific point on the page

I am trying to automate an online survey on a website, but I get this error every 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 "

I need to understand how I can scroll to a certain point so that my script can resume filling out the survey on the page.

This is my code that allows me to fill out part of the survey, but it fails when it reaches a line that does not appear inside the browser (a line that requires the user to scroll down):

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

buttons.each do |button|
  button.click
end

I would also like to be able to change my code so that it selects only a specific parameter, but the HTML on the page is not very friendly.

This is the webpage I'm looking at: https://staging2.clearfit.com/assessment/assessment/95867fb272df436352a0bd5fbdd

HTML of one of the survey options:

<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>
+4
3

execute_script

, javascript:

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

, , script. , .

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

, , . :

browser.scroll.to button

script :

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
+7

-, . , . - , , Selenium javascript:

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

buttons.each do |button|
  button.wd.location_once_scrolled_into_view
  button.click
end
+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), "" .

- , . , (0,0); , .

0

All Articles