Watir. Scroll to a certain point of the page
Asked Answered
K

3

7

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

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.

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

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>
Kalasky answered 18/11, 2013 at 21:9 Comment(6)
I am not seeing that error. Can you provide a complete script that reproduces the problem (ie to make it more clear where the exception occurs)?Haematin
Provide more details.Rimarimas
The code that I have was typed into irb so I don't have a complete script as of this moment. If I am to fill out the entire survey I have to scroll down since my screen doesn't fit the entire page. Sorry I am a bit unclear as to what else I can provide.Kalasky
Since your page is publicly available, it should be possible to create a standalone script that we can run to see the exact error you are seeing (ie if we copy the code and then run it, we should see the same error). So far, we cannot reproduce your problem with the information provided. Part of my confusion is you say that the button clicking script provided works... but there is no other code given, so what code is actually failing?Haematin
Sorry about that. I will clarify that. The code I provided is the only code I have and I am getting the error I described above. The code manages to fill out some of the survey but as soon as it gets to the row which requires the user to scroll down to it give this error.Kalasky
Okay, I figured out why I could not reproduce the problem. It does not occur when using Firefox, but can be seen when using Chrome.Haematin
H
10

Using execute_script

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

Using the watir-scroll 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

The script would then look like:

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
Haematin answered 19/11, 2013 at 17:58 Comment(6)
it gives error undefined method scroll' for #<Watir::Browser:0x314d858> (NoMethodError)Bluebill
Did you actually install the watir-scroll gem? browser.scroll is not part of the standard Watir API.Haematin
Yes I did install it. I updated the gemfile with line gem 'watir-scroll'and then bundle install.Bluebill
Did you also require it - ie require 'watir-scroll'?Haematin
browser.scroll.to button did not work for me but button.scroll.to did. That is the format the gem README usesMd
Will this just scroll down or scroll up as well wherever the element is present?Vallombrosa
S
2

Firstly, this should be unnecessary. According to the spec, all element interactions require implicit scrolling to the element. If something does prevent this from happening, though, you can use this Selenium method instead of a javascript implementation:

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

buttons.each do |button|
  button.wd.location_once_scrolled_into_view
  button.click
end
Switzerland answered 5/11, 2015 at 15:44 Comment(0)
P
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

Then you just call jump_to(element) and it "Jumps" to it.

This is how I got around it- not sure if that is a normal way. The problem is it goes to point (0,0); working on a version that moves to it to center screen.

Parcheesi answered 1/11, 2017 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.