使用Selenium Webdriver向下滚动页面 [英] Scrolling down a page with Selenium Webdriver
问题描述
我有一个动态页面,当用户向下滚动页面时,该页面会加载产品.我想获取显示页面上呈现的产品总数.目前,我正在使用下面的代码进入最底层,直到显示所有产品为止.
I have a dynamic page that loads products when the user scrolls down a page. I want to get the total number of products rendered on the display page. Currently I am using the following code to get to the bottom until all the products are being displayed.
elems = WebDriverWait(self.driver, 30).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "x")))
print len(elems)
a = len(elems)
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(4)
elem1 = WebDriverWait(self.driver, 30).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "x")))
b = len(elem1)
while b > a:
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(4)
elem1 = WebDriverWait(self.driver, 30).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "x")))
a = b
b = len(elem1)
print b
这很好,但是我想知道是否有更好的选择吗?
推荐答案
您可以使用此行代码轻松执行此操作
You can perform this action easily using this line of code
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
如果您想永远向下滚动,则应尝试此操作.
And if you want to scroll down for ever you should try this.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get("https://twitter.com/BarackObama")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
我不确定关于时间.sleep(x值)导致加载数据的时间更长或更短.. 有关更多信息,请检查
I am not sure about time.sleep(x value) cause loading data my take longer .. or less .. for more information please check the official Doc page
玩得开心:)
这篇关于使用Selenium Webdriver向下滚动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!