rubycucumberwatirwatir-webdriver

how to scroll a web page using watir


I am trying to scroll a web page to find and click on a content that is lazily loaded when the page is scrolled. I am using following command

require 'watir-webdriver'

@browser = Watir::new :firefox
@browser.send_keys :space

I am using web-driver with firefox and I am on ubuntu but it is not working. In the following ruby code I am trying to scroll the page down until I don't find the element with :id. The element is loading lazily. I am getting timeout after few seconds, any idea what is wrong with the following code.

When /^deal (\d+) is loaded$/ do |id|
  (0..5).each do |click|    
    @browser.send_keys :space
  end
end

What is the best way to scroll a page using watir-driver ?


Solution

  • If you have JavaScript enabled, you can access the underlying driver and execute some JavaScript to scroll on the page.

    @browser.driver.executeScript("window.scrollBy(0,200)")
    

    will scroll down the page 200 pixels along the y axix.

    See here for documentation of the method:

    http://www.w3schools.com/jsref/met_win_scrollby.asp