jobs = driver.find_elements(By.CLASS_NAME, "job-name") for j in job: print(j.text)
6. 等待页面加载
在抓取动态加载内容时,可能需要等待页面加载。可以使用显式等待和隐式等待。
隐式等待:设置一个全局等待时间。
1
driver.implicitly_wait(10)
显式等待:等待特定条件满足。
1 2 3 4 5 6
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10) element = wait.until(EC.presence_of_element_located((By.ID, "element_id")))
# 导入库 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import StaleElementReferenceException