Selenium 元素等待
概念
- 显示等待是针对某一个元素进行相关等待判定;
- 隐式等待不针对某一个元素进行等待,全局元素等待;
- 强制等待通过sleep来实现进程等待。
相关模块
- WebDriverWait 显示等待针对元素必用
- expected_conditions 预期条件类(下面很多方法可以调用,用于显示等待)
- NoSuchElementException 用于隐式等待抛出异常
- By 用于元素定位
模块引用
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 from selenium.common.exceptions import NoSuchElementException
显示等待
案例:检测百度页面搜索按钮是否存在,存在就输入关键词“Selenium” 然后点击搜索
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from time import sleep driver=webdriver.Chrome() driver.get("http://www.baidu.com") driver.find_element_by_css_selector(".s_ipt").send_keys("Selenium") sleep(2) #显示等待--判断搜索按钮是否存在 element=WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,"su"))) element.click() sleep(3) driver.quit()
隐式等待
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from time import sleep,ctime driver=webdriver.Chrome() driver.get("http://www.baidu.com") sleep(2) driver.implicitly_wait(5) #隐式等待时间设定 5秒 #检测搜索框是否存在 try: print(ctime()) driver.find_element_by_css_selector("#kw22").send_keys("Python") except NoSuchElementException as msg: print(msg) finally: print(ctime()) sleep(3) driver.quit()
isdisplay()方法来判断元素是否显示
for i in range(10): el = driver.find_element_by_id("kw22") try: if el.isdisplyed(): break except: pass else: # 注意 有try except嵌套时,if else 不一定是和if对其 print("timeout")