等待多个元素 放入一个列表 WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"XX")))
这里需要百度的热点新闻标题 是多个元素
# 导包 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 定义driver driver=webdriver.Chrome() # 打开浏览器 driver.get("https://www.baidu.com") # 获取元素 elements=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"#s-hotsearch-wrapper [class='title-content-title']"))) print(elements) for e in elements: print(e.text) # 退出 driver.quit()
最终效果
[<selenium.webdriver.remote.webelement.WebElement (session="507658923d6cc8d0d870104261cef1c7", element="488DD4F78E9932A954C4E27EDDF4C8AF_element_8")>, <selenium.webdriver.remote.webelement.WebElement (session="507658923d6cc8d0d870104261cef1c7", element="488DD4F78E9932A954C4E27EDDF4C8AF_element_9")>, <selenium.webdriver.remote.webelement.WebElement (session="507658923d6cc8d0d870104261cef1c7", element="488DD4F78E9932A954C4E27EDDF4C8AF_element_10")>, <selenium.webdriver.remote.webelement.WebElement (session="507658923d6cc8d0d870104261cef1c7", element="488DD4F78E9932A954C4E27EDDF4C8AF_element_11")>, <selenium.webdriver.remote.webelement.WebElement (session="507658923d6cc8d0d870104261cef1c7", element="488DD4F78E9932A954C4E27EDDF4C8AF_element_12")>, <selenium.webdriver.remote.webelement.WebElement (session="507658923d6cc8d0d870104261cef1c7", element="488DD4F78E9932A954C4E27EDDF4C8AF_element_13")>] 察浙江纪实 奋力谱写中国式现代化浙江新篇章 空前火爆的中秋国庆假期 姚明赢球后笑了 今秋将形成中等强度厄尔尼诺事件 同居生子女子要求每月1元抚养费