Watir. Scroll to a certain point of the page

最后都变了- 提交于 2019-11-30 07:23:31

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!