how to scroll a web page using watir
Asked Answered
J

7

15

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 ?

Jermainejerman answered 28/11, 2012 at 15:44 Comment(0)
A
22

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

Adina answered 28/11, 2012 at 16:6 Comment(1)
Note that you do not have to access the driver to execute the script. You can just do: @browser.execute_script("window.scrollBy(0,200)").Erminia
J
7

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]
Jahvist answered 7/1, 2015 at 22:46 Comment(3)
it gives undefined method scroll for #<Watir::Browser:0x314d858> (NoMethodError)Gorky
@Gorky - same here. i am using ruby 1.8.7, watir 1.6.7, watir-webdriver 0.6.2Denominational
Double check that you installed the gem 'watir-scroll'. Works fine now, on watir 6.15.1 . watir-scroll 0.4.0Cowan
C
4

This saved me a bunch of time:

browser.div(:id => 'start-date-holder').wd.location_once_scrolled_into_view 
Cerebellum answered 4/8, 2016 at 16:46 Comment(0)
R
2

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.

Rozek answered 6/1, 2015 at 18:39 Comment(1)
why not wait_until{block}? hardcoded wait methods = irritatingZarf
D
1
    300.times do
      @browser.driver.execute_script("window.scrollBy(0,200)")
      sleep 0.05
    end
Drown answered 20/3, 2020 at 7:47 Comment(1)
Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of better quality, and are more likely to attract upvotes.Felafel
T
0

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

@browser.driver.execute_script( "window.scrollBy(0,document.body.scrollHeight)" )  

which scrolls to the bottom of the page on the y-axis.

Therron answered 17/4, 2017 at 0:17 Comment(0)
A
0

It works

evaluate_script("document.getElementsByTagName('body')[0].scrollTop=0;")
Abnaki answered 7/6, 2017 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.