如何在红宝石中使用硒向下滚动到底部 [英] How to scroll down to bottom using selenium in ruby

查看:54
本文介绍了如何在红宝石中使用硒向下滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我如何在 ruby​​ 中使用 selenium 向下滚动到页面底部吗?我读了这个

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!

推荐答案

当我查看页面时,我没有看到带有类页脚的 div.这可能就是您找不到该元素的原因.

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

对我来说,最后一个可见元素似乎是壁纸——即带有 pic 类的 div.您可以获得最后一张图片并使用以下方法滚动到它.请注意,我们找到所有图片,然后拍摄集合中的最后一张.

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

这篇关于如何在红宝石中使用硒向下滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆