javascript - 如何使用 selenium 向下滚动屏幕?
问题描述
我正在尝试用硒滚动我的屏幕以捕捉一些元素,但我不能这样做......
我用这个:
等待 driver.executeScript("window.scrollBy(0,450)", "");
和这个:
常量页面 = 等待 seleniumUtils.getElementByCSS('body'); 等待 seleniumUtils.click(page); 等待 page.sendKeys(Key.COMMAND,Key.DOWN);
没有任何效果!我需要帮助,请:)
解决方案
只是一个 python 解决方案,用于滚动无限滚动网站。
随意编辑/重写以供您使用...
第 1 步:创建“滚动”功能
def scroll(driver, timeout):
scroll_pause_time = timeout
# Get scroll height
last_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:
# If heights are the same it will exit the function
break
last_height = new_height
第 2 步:创建“滚动”功能后,您可以通过以下方式使用它:
from selenium import webdriver
driver = webdriver.Firefox() # create driver
driver.get('www.yoururl.com') # navigate url
scroll(driver, 5) # use scroll function to scroll the page every 5 second (usefull for infinite scrolling websites)
推荐阅读
- asp.net - 在每个角度组件加载时调用 JWT 刷新令牌 API
- azure-devops - 更改用于创建工作项的 git 提交链接的字符
- javascript - 提交表单时如何检查用户电子邮件是否已在我的数据库中?
- javascript - 多个声明时变量值填充不正确
- c++ - 在 sizeof 运算符上强制编译时错误
- javascript - 为什么ajax不通过图像文件?
- rxjs - 我应该在 jest/jasmine 测试中取消订阅 Observable 吗?
- java - 我无法理解为什么这段代码标记了一些东西
- java - 即使依赖项存在于类路径中,Spring boot 仍显示“无法解析”
- javascript - 如何将 settimeout 与 vue.js 观察者一起使用?