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 ?
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.
You can either scroll to a specific element
Or just scroll to the top middle or center
Or scroll to a coordinate
It works
If you have JavaScript enabled, you can access the underlying driver and execute some JavaScript to scroll on the page.
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
You can access the underlying driver and execute some javascript. For instance if you wanted to scroll to the bottom of the page you would use
which scrolls to the bottom of the page on the y-axis.
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):
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.This saved me a bunch of time: