How to scroll down to bottom using selenium in rub

2019-09-18 05:17发布

问题:

Can you show me the way to use selenium in ruby to scroll down to bottom of page? I read this

element = driver.find_element(:xpath, "//div[@class='footer small']")
element.location_once_scrolled_into_view

but in this link, i can't find any element. Can you show me the way without element like that found. Thank you!

回答1:

When I looked at the page, I did not see a div with class footer. This might be why you cannot find the element.

For me, the last visible element appears to be the wallpapers - ie div with class pic. You can get the last picture and scroll to it using the following. Note that we find all of the pictures and then take the last one in the collection.

last_picture = driver.find_elements(:css, 'div.pic').last
last_picture.location_once_scrolled_into_view

After you scroll to the last wallpaper, you will want to wait for the page to finish loading. For example, the following will wait until the image count increases:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.navigate.to 'http://www.mobileswall.com/'

# Check how many elements are there initially  
puts driver.find_elements(:css, 'div.pic').length
#=> 30

# Scroll to the last image
driver.find_elements(:css, 'div.pic').last.location_once_scrolled_into_view

# Wait for the additional images to load
current_count = driver.find_elements(:css, 'div.pic').length
until current_count < driver.find_elements(:css, 'div.pic').length
  sleep(1)
end

# Check how many elements are there now
puts driver.find_elements(:css, 'div.pic').length
#=> 59