- 显式等待
在设置时间内,每间隔一段时间检查一次当前页面元素是否存在,如超过设置检查时间检查不到就抛出异常。
from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.get("https://www.***.cn")#设置最长超时时间5,检测间隔0.5ele = 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 webdriverfrom selenium.webdriver.common.by import Bydriver = 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 webdriverfrom selenium.webdriver.common.by import Byfrom time import sleepdriver = webdriver.Chrome()driver.get("https://www.***.cn")sleep(5)driver.find_element(By.CSS_SELECTOR,"***").send_keys("***")driver.quit()