显示等待
使webdriver等待某个条件成立时继续执行,否则在最大时长时抛出超时溢出
1 from selenium.webdriver.support.wait import WebDriverWait 2 from selenium.webdriver.support import expected_conditions as EC 3 4 def find_element(self,*loc): 5 '''寻找元素''' 6 try: 7 WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc)) #显示等待 8 return self.driver.find_element(*loc) 9 except: 10 print('%s页面未找到元素'% loc)
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)
driver:浏览器驱动
timeout:最长超时时间,单位秒
poll_frequency:检测的间隔时长,默认0.5s
ignored_exceptions:超时后的异常信息,默认情况下抛NoSuchElementException
WebDriverWait()一般由unitl()或until_not()方法配合使用
until()调用该方法提供的驱动作为一个参数,直到返回值为True
unitl_not()调用该方法提供的驱动作为一个参数,直到返回值为False
隐式等待
等待页面上某元素加载完成。如果超出设置的时长,抛出NoSuchElementException异常。
使用:driver.implicitly_wait() 默认设置为0
eg:driver.implicitly_wait(10) 。如果元素在10s内定位到了,继续执行。如果定位不到,将以循环方式判断元素是否被定位到。如果在10s内没有定位到,则抛出异常