how to scroll a web page using watir

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


This saved me a bunch of time:

browser.div(:id => 'start-date-holder').wd.location_once_scrolled_into_view 

I use a gem called "watir-scroll" to assist me with this. Though it usually needs places to scroll to, it also will scroll to coordinates.

https://github.com/p0deje/watir-scroll Since Watir v6.16 watir-scroll gem merged into watir

You can either scroll to a specific element

button1 = @browser.input(:class => "mileage_rate")
@browser.scroll.to button1

Or just scroll to the top middle or center

@browser.scroll.to :top
@browser.scroll.to :center
@browser.scroll.to :bottom

Or scroll to a coordinate

browser.scroll.to [0, 200]

Sorry I could not comment on the last answer since I am new here and do not have enough rep pts yet so I just created a new answer. Anyways, if anyone is having issues with scrolling multiple times try this (add a loop and sleep):

maximum_times_needed = max # of times you need the page to scroll down

maximum_times_needed.each do
@browser.driver.executeScript("window.scrollBy(0,200)")
sleep 0.15
end

0.15 may vary depending on how long it takes the page to load. 0.15 is 0.15 seconds so adjust as needed to allow for enough time for the page to load. The 200 may also need to be adjusted to a larger pixel amount.