加入等待时间,主要是考虑到网页加载须要时间,可能因为网速慢,或者使用了 ajax
技术实现了异步加载等,若是程序找不到指定的页面元素,就会致使报错发生。python
经常使用的有3种等待方式:web
- 强制等待
- 隐式等待
- 显示等待
使用 Python 自身的库 time.sleep()
能够实现强制等待。ajax
强制等待使用简单,可是,当网络条件良好的时候,建议减小使用,由于若是频繁使用强制等待的方式等待元素加载,会致使整个项目的自动化时间延长。浏览器
这种等待方式的使用场景主要是脚本调试。网络
隐式等待其实是,设置了一个最长的等待时间,若是在这段时间内可以定位到目标,则执行下一步操做,不然会一致等到规定时间结束,而后再执行下一步。dom
隐式等待设置一次,对整个 driver 周期都可以起做用,因此,在最开始设置一次便可。异步
注意:在同一个 driver 周期中遇到强制等待,可能会致使隐式等待失效
# 隐式等待,京东的“新人福利” from selenium import webdriver from time import sleep driver = webdriver.Chrome() # 打开浏览器 driver.maximize_window() # 浏览器最大化 driver.get("https://www.jd.com/") # 跳转至京东 driver.implicitly_wait(10) # 隐式等待 10s element = driver.find_element_by_xpath("//*[@class='user_profit_lk']") # 定位元素 element.click() # 点击 sleep(3) driver.quit() # 关闭浏览器
WebDriverWait
是 Selenium 提供的显式等待的模块,使用原理是:在指定的时间范围内,等待到符合/不符合某个条件为止。测试
导入方式:ui
from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait
参数:spa
序号 | 参数 | 描述 |
---|---|---|
1 | driver | 传入的 WebDriverWait 实例 |
2 | timeout | 超时时间,等待的最长时间 |
3 | poll_frequency | 调用 until 或 until_not 中的方法的间隔时间(默认是0.5秒) |
4 | ignored_exceptions | 忽略的异常 |
WebDriverWait
模块含有两个方法:
- until
- until_not
until 与 until_not 的参数:
序号 | 参数 | 描述 |
---|---|---|
1 | method | 在等待期间,每隔一段时间调用这个传入的方法,直到返回值不为 False |
2 | message | 若是超时,抛出 TimeoutException ,将 message 传入异常 |
一般状况下,WebDriverWait
模块会与 expected_conditions
模块搭配使用,用来写入 until 与 until_not 中的参数——method。
expected_conditions
模块有如下等待条件:
序号 | 等待条件方法 | 描述 |
---|---|---|
1 | title_is(object) | 判断标题,是否出现 |
2 | title_contains(object) | 判断标题,是否包含某些字符 |
3 | presence_of_element_located(object)--经常使用 | 判断某个元素,是否被加到了 dom 树里,并不表明该元素必定可见 |
4 | visibility_of_element_located(object)--经常使用 | 判断某个元素,是否被加到了 dom 树里,而且可见,宽和高都大于0 |
5 | visibility_of(object) | 判断元素是否可见,若是可见则返回这个元素 |
6 | presence_of_all_elements_located(object) | 判断是否至少有1个元素存在 dom 树中 |
7 | visibility_of_any_elements_located(object) | 判断是否至少有1个元素在页面中可见 |
8 | text_to_be_present_in_element(object) | 判断指定的元素中是否包含了预期的字符串 |
9 | text_to_be_present_in_element_value(object) | 判断指定元素的属性值是否包含了预期的字符串 |
10 | frame_to_be_available_and_switch_to_it(object) | 判断该 frame 是否能够 切换进去 |
11 | invisibility_of_element_located(object) | 判断某个元素是否存在与 dom 树中或不可见 |
12 | element_to_be_clickable(object) | 判断某个元素中是否可见,而且是可点击的 |
13 | staleness_of(object) | 等待某个元素从 dom 树中删除 |
14 | element_to_be_selected(object) | 判断某个元素是否被选中,通常用在下拉列表中 |
15 | element_selection_state_to_be(object) | 判断某个元素的选中状态是否符合预期 |
16 | element_located_selection_state_to_be(object) | 判断某个元素的选中状态是否符合预期 |
17 | alert_is_present(object) | 判断页面上是否出现 alert 弹窗 |
# 模拟场景:点击京东首页的“新人福利” from selenium import webdriver from time import sleep 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.maximize_window() # 浏览器最大化 driver.get("https://www.jd.com/") # 跳转至京东 element = WebDriverWait(driver, 20, 0.5).until( EC.visibility_of_element_located((By.XPATH, "//*[@class='user_profit_lk']")) ) # 20秒内,直到元素在页面中可定位 element.click() # 点击 sleep(3) driver.quit()
显式等待,虽然使用起来,相比其余等待方式,显得要复杂,可是它的优点在于灵活,经过封装后,经过简单的调用,就能够运用到自动化测试项目中。