Python Selenium页面向下滚动该页面分为两个,两个scollers

问题描述

我希望向下滚动Facebook Messenger上的对话部分。

页面包含2个滚动条,我想向下滚动1号滚动条查看图片

这是页面链接(您应该已登录,并且有25条以上的消息):https://www.facebook.com/messages/t/

enter image description here

我的实际代码

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。 箭头键在滚动条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);")