简体   繁体   English

如何在python中使用Selenium Webdriver滚动div

[英]How to scroll a div using selenium webdriver in python

I'm trying to write a script that prints out all the active friends from facebook web. 我正在尝试编写一个脚本,以打印出来自Facebook网站的所有活跃朋友。 I'm having difficulty scrolling the div that contains the active friends list 我在滚动包含活动好友列表的div时遇到困难

I was trying to scroll the active chat list on facebook and its not working 我试图在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'})")

JavascriptException: Message: javascript error: scroll_active_friends_list is not defined (Session info: chrome=75.0.3770.142) JavascriptException:消息:javascript错误:scroll_active_friends_list未定义(会话信息:chrome = 75.0.3770.142)

I would use the WebDriverWait instead of implicit wait for the elements to be loaded and location_once_scrolled_into_view method that will scroll to the element. 我将使用WebDriverWait而不是隐式等待元素加载以及将滚动到该元素的location_once_scrolled_into_view方法。

Imports needed: 需要进口:

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

Script 脚本

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        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM