- 显式等待
在设置时间内,每间隔一段时间检查一次当前页面元素是否存在,如超过设置检查时间检查不到就抛出异常。
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.get("https://www.***.cn") #设置最长超时时间5,检测间隔0.5 ele = WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.CSS_SELECTOR,"***"))) ele.send_keys("***") driver.quit()
- 隐式等待(implicitly_wait)
通过一定时长等待页面上某元素加载完成,默认是0,如超出了设置的时长元素还没加载完成,则抛出NoSuchElementException异常。
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.implicitly_wait(5) driver.get("https://www.***.cn") driver.find_element(By.CSS_SELECTOR,"***").send_keys("***") driver.quit()
- sleep休眠
from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep driver = webdriver.Chrome() driver.get("https://www.***.cn") sleep(5) driver.find_element(By.CSS_SELECTOR,"***").send_keys("***") driver.quit()