javascript - 在 python selenium javascript 中缓慢滚动

标签 javascript python selenium web-scraping

url = correct_url(url)
browser = webdriver.Chrome()
browser.get(url)
browser.find_element_by_xpath('//*[@title="New chat"]').click()
drawer_body = browser.find_elements_by_class_name('drawer-body')
browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', drawer_body)

Get the 'div' element, this has a list and will be scrolled

drawer_body = browser.find_elements_by_class_name('抽屉体')

The line below scrolls very fast and reaches the bottom but this does not load data. Is there any way I can slowly scroll into drawer_body element?.

browser.execute_script('参数[0].scrollTop = 参数[0].scrollHeight',抽屉体)

我正在尝试使用 python 和 selenium 实现以下脚本。 http://ctrlq.org/code/19966-whatsapp-phone-numbers

最佳答案

大多数浏览器支持空格键键作为向下滚动页面的键盘快捷键。使用此功能,您可以执行以下操作:

  1. 访问页面
  2. 解析目前加载的内容
  3. 使用 Selenium 模拟按空格键
  4. 等待 2-3 秒,直到加载更多数据
  5. 继续

还有一个提示:为了避免陷入永远循环,您需要检查新加载的数据是否与按空格键之前的数据不同,否则脚本应该退出,因为没有新数据可供解析。

关于javascript - 在 python selenium javascript 中缓慢滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38773273/

相关文章:

python - 该matlab代码对应的matplotlib代码是什么

node.js - 你如何在循环中为不同的浏览器运行 webdriver io 测试?

c# - 如何在 C# 中为 SafariDriver 设置 CleanSession 功能?

javascript - JQuery - 自动刷新(复制表单)

javascript - did() 是什么,它的文档在哪里?

Python猜谜游戏,用字母替换空格并存储在列表中

jquery - 使用 Selenium 测试 jQuery Drag & Drop 和 Droppable

javascript - 期望数组相等而忽略顺序

javascript - react webpack 配置 : is it possible to replace config for only one plugin in array, 而不重置插件数组?

python - 在 Python 中对字典使用比较运算符