python - Python Selenium Page Scroll Down(页面分为两个,2个scollers)
问题描述
我正在寻找向下滚动 facebook messenger 上的对话部分。
该页面包含 2 个滚动条,我希望向下滚动滚动条编号 1检查图片
这是该页面的链接(您应该登录并拥有超过 25 条消息):https ://www.facebook.com/messages/t/
我的实际代码:
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
driver = webdriver.Chrome('chromedriver path', options=opt)
# go to fb
driver.get("https://www.facebook.com/")
time.sleep(5)
# login to fb
email = driver.find_elements_by_xpath("//input[@name='email']")
email.sendkeys("youremail")
pass = driver.find_elements_by_xpath("//input[@name='pass']")
pass.sendkeys("yourpassword")
time.sleep(3)
# go to facebook messenger
driver.get("https://www.facebook.com/messages/t/")
# this is what i've tried but didn't work
# start scroling
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)
解决方案
我们有很多选项可以向下滚动,但是我手动检查了 fb。Page down、end、down 箭头键在滚动条 1 上不起作用。
在这种情况下,选项 1 javascript 执行程序可能会有所帮助,并在您的信使中使用您好友的姓名。
element=driver.find_element_by_xpath('//span[contains(text(),'nameofyourbuddy')]//ancestor-or-self::div[position()=3]')
driver.execute_script("arguments[0].scrollIntoView();", element)
或使用元素滚动到底部页面
element=driver.find_element_by_xpath("//*[@aria-label='Conversation List']/child::*[last()]")
driver.execute_script("arguments[0].scrollIntoView();", element)
如果您没有任何要向下滚动的特定元素,则选项 2 滚动页面底部
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
推荐阅读
- java - 用Java计算Excel一列的重复行数
- python-3.x - 如何从 Colab Notebook 中提取 Google Cloud Storage 存储桶中的 tar.gz 文件?
- sql - 如何为每个列值选择不同的项目数?
- birt - 在 AIX 上,BIRT 看到字体,rotatedtext 控件没有
- firebase - 如何在 us-central1 以外的区域使用 httpsCallable for web
- javascript - 向帖子添加新评论时“超出最大调用堆栈大小”
- r - 未使用 scale_fill_discrete 值
- php - 我从同一网络中的不同计算机获取相同的 IP 地址
- html - 为什么这个 html 'aside' 没有出现在侧面?
- node.js - 数据在数据库中发生变异但未更新