如何在python中使用Selenium网络驱动程序滚动div



我正在尝试编写一个脚本,从Facebook网络上打印出所有活跃的朋友。我在滚动包含活动好友列表的div 时遇到困难

我试图在Facebook上滚动活动聊天列表,但它不起作用

username = driver.find_element_by_id("email").send_keys(input_email)
password = 
driver.find_element_by_id("pass").send_keys(input_password)
login = driver.find_element_by_id("loginbutton").click()
driver.implicitly_wait(40)
scroll_active_friends_list = driver.find_element_by_id("u_0_1y")
driver.execute_script("scroll_active_friends_list.animate({scrollTop: 
'100px'})")

Javascript异常:消息:javascript错误:未定义scroll_active_friends_list (会话信息:chrome=75.0.3770.142(

我会使用 WebDriverWait 而不是隐式等待加载元素并location_once_scrolled_into_view将滚动到元素的方法。

需要进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

脚本

username = driver.find_element_by_id("email").send_keys(input_email)
password = driver.find_element_by_id("pass").send_keys(input_password)
login = driver.find_element_by_id("loginbutton").click()
# wait for the div to be present
scroll_active_friends_list = WebDriverWait(driver,40).until(EC.presence_of_element_located((By.ID("u_0_1y"))))
scroll_active_friends_list.location_once_scrolled_into_view        

相关内容

最新更新