Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 680
amjad Avatar asked Nov 28 '12 15:11

amjad


People also ask

What is Watir used for?

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.

Is Watir a test management tool?

Watir is one of the best regression testing tools as writing, maintaining/updating and executing test cases are easy.

What is Watir WebDriver gem?

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


2 Answers

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

like image 57
CBA Avatar answered Oct 19 '22 22:10

CBA


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]
like image 38
John Escobedo Avatar answered Oct 19 '22 22:10

John Escobedo