等待时间用法
1.显式等待:WebDriverWait
1)设置定义:设置时间内默认每个一段时间检测一次设置的元素是否存在,满足条件继续,如果超时则抛出异常。
2)格式:WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
driver:”浏览器驱动
timeout:最长超时时间,默认以秒为单位
poll_frequency:检测的间隔时间,默认为0.5s
ignored_exceptions:超时后异常信息
3)用法:WebDriverWait(driver, 10, 0.5, ignored_exceptions="超时").until(expected_conditions.presence_of_element_located((By.ID,"kw")))
4)备注:在使用此方法时需要导入方法包:from selenium.webdriver.support.ui import WebDriverWait 和 from selenium.webdriver.support import expected_conditions.使用By方法需要导入方法包from selenium.webdriver.common.by import By
2.隐式等待:implicitly_wait()
1)设置定义:以轮询的方式判断元素是否被定位到,定位到继续,超时抛出异常,定位的元素为在其方法下的所需定位的元素
2)格式:driver.implicitly_wait(timeout)
3)用法:driver.implicitly_wait(10)
4)备注:用此方法需要导入异常包 from selenium.common.exceptions improt NoSuchElementException 和时间包 from time import ctime
3.睡眠方法:sleep
1)设置定义:设置休眠时间
2)格式:sleep(timeout)
3)用法:sleep(5)
4)备注:使用此方法需要导入方法包 from time import sleep
浙公网安备 33010602011771号