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 ?
Definition: Watir, pronounced as water, is a group of Ruby libraries for automated web browsers. It allows writing the tests which are easy to read and maintain. In other words, it is a simple and flexible tool. Description: Watir drives the browsers the same way as people do.
Watir is one of the best regression testing tools as writing, maintaining/updating and executing test cases are easy.
Watir-WebDriver (Watir is short for Web Application Testing in Ruby) is a Ruby gem which allows you to automate your browser (make it click a button, submit a form, wait for some text to appear before continuing, and so on).
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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With