如何在滚动时获得动态更新的元素列表?



我试图通过不和谐的成员列表循环,但这个列表只返回22个成员(membersSideBar),我想我需要手动滚动这个列表是完整的,但我不知道如何,这是我的Python代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
import time
driver = webdriver.Chrome("H:ProgsMz's FlashProgGoogle Chromechromedriver_x86_v93.exe", options=o)
driver.get("https://discord.com/channels/753059977689694218/753059977689694222")
email = driver.find_element_by_name("email")
email.send_keys('abc@example.com')
password = driver.find_element_by_name("password")
password.send_keys('pa$$word')
password.send_keys(Keys.RETURN)
membersSideBar = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "clickable-1JJAn8")))
for i in membersSideBar:
time.sleep(2)
driver.execute_script("arguments[0].scrollIntoView();", i)
i.click()

尝试如下操作并确认:

设置一个变量来跟踪membersSideBar

While循环locate the Element并保持scrolling to the ith Element

滚动时,membersSideBar更新了新的元素列表。

如果没有找到更多的元素,将抛出list out of bound异常,因此将代码块放在try and except块中。

i = 0
try:
while True:
membersSideBar = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "clickable-1JJAn8")))
driver.execute_script("arguments[0].scrollIntoView(true);", membersSideBar[i])
time.sleep(2)
i += 1
except:
pass

相关内容

  • 没有找到相关文章