如何在python中使用Selenium Webdriver滚动网页?

如何在python中使用Selenium Webdriver滚动网页?,第1张

如何在python中使用Selenium Webdriver滚动网页?

你可以使用

driver.execute_script("window.scrollTo(0, Y)") 

其中Y是高度(在全高清显示器上为1080)。

你也可以使用

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

滚动到页面底部

如果你想滚动到无限加载的页面,例如社交网络页面,facebook等

SCROLL_PAUSE_TIME = 0.5# Get scroll heightlast_height = driver.execute_script("return document.body.scrollHeight")while True:    # Scroll down to bottom    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")    # Wait to load page    time.sleep(SCROLL_PAUSE_TIME)    # Calculate new scroll height and compare with last scroll height    new_height = driver.execute_script("return document.body.scrollHeight")    if new_height == last_height:        break    last_height = new_height

另一种方法是,选择一个对象,然后

label.sendKeys(Keys.PAGE_DOWN);


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4890111.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-11
下一篇 2022-11-11

发表评论

登录后才能评论

评论列表(0条)

保存