首页 > 解决方案 > 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)

标签: pythonhtmlseleniumwebdriver

解决方案


我们有很多选项可以向下滚动,但是我手动检查了 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);")

推荐阅读