如今大多数Web应用程序使用Ajax技术,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成的,这给元素的定位增加了困难。如果因为在加载某个元素时延迟而造成ElementNotVisibleException的情况出现,那么就会降低自动化脚本的稳定性,我们可以通过设置元素等待改善这种问题造成的不稳定。

WebDriver提供了两种类型的等待:含蓄等待明确等待。明确等待作用于特定代码块,使得WebDriver等待某个条件成立时继续执行,否则在达到最大时长时抛出超时异常;而含蓄等待,属于全局超时设置,则会让WebDriver在指定的时间内不断轮询DOM尝试定位元素,直到成功定位元素或超时。

明确等待

方式一:比较极端的方式是通过time.sleep() 来让程序休眠指定时间,然后继续执行。

方式二:结合WebDriverWait和ExpectedCondition 来设置等待。WebDriverWait 类是有WebDriver提供的等待方法,在设置时间内,默认每隔一段时间检测一次当前页面元素是否存在。具体格式如:WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)

#!/usr/bin/env python
# -*- coding: utf-8 -*- 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
import time driver = webdriver.Chrome()
driver.get("http://www.baidu.com") wait = WebDriverWait(driver, 5, 0.5)
element = wait.until(EC.presence_of_element_located((By.ID, "kw")))
element.send_keys("selenium")
time.sleep(5)
driver.close()

如上代码块,打开百度首页后,除非能定位到ID为“kw”的控件才继续往下执行,否则将会一直等待到5s后抛出超时异常。WebDriverWait默认每500毫秒去检查一次ExpectedCondition

expected_conditions类所提供的预期条件判断的方法如下:

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of_element_located
  • visibility_of
  • presence_of_all_elements_located
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • invisibility_of_element_located
  • element_to_be_clickable
  • staleness_of
  • element_to_be_selected
  • element_located_to_be_selected
  • element_selection_state_to_be
  • element_located_selection_state_to_be
  • alert_is_present

含蓄等待

An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object.

implicitly_wait 属于全局智能等待时间,一旦设置,作用于整个WebDriver生命周期,它不是固定的等待时间,一旦定位到元素就继续往下执行。

#!/usr/bin/env python
# -*- coding: utf-8 -*- from selenium import webdriver driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://www.baidu.com") element = driver.find_element_by_id("kw")
element.send_keys("selenium")
driver.close()

***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***

最新文章

  1. 使用doxygen制作C代码文档
  2. 【Codeforces 707C】Pythagorean Triples(找规律)
  3. QQ空间HD(2)-UIPopoverController其它使用
  4. [家里蹲大学数学杂志]第056期Tikhonov 泛函的变分
  5. Java---XML的解析(2)-DOM4J解析/Xpath
  6. Water Tree
  7. Apache Kylin
  8. Python中关于try...finally的一些疑问
  9. 深入理解Solaris内核中互斥锁(mutex)与条件变量(condvar)之协同工作原理
  10. LPC1768的USB-相关结构体定义
  11. .Net MVC&&datatables.js&&bootstrap做一个界面的CRUD有多简单
  12. javassist:字节码编辑器工具
  13. python从入门到实践-5章if语句
  14. 1.SpringMVC入门
  15. Machine Learning - Andrew Ng - Coursera
  16. Zookeeper浏览器工具和Eclipse插件
  17. 通过javac导出Jar包
  18. NuGet:自定义配置信息(2)
  19. 关于linux系统端口查看和占用的解决方案
  20. Uva10917 Walk Through the Forest

热门文章

  1. The Ultimate Guide To A/B Testing
  2. 微信js获取地理位置
  3. 深入理解CNI
  4. error:No resource found that matches the given name 'Theme.AppCompat.Light'
  5. linux 或c 时间相关处理类型和函数
  6. Windows Live Writer 网易博客配置
  7. Spring框架学习之IOC(一)
  8. Entity FrameWork Code First 之Model分离
  9. Lightroom’s Clarity Slider – What Does It Do?
  10. MySQL Binlog解析(1)