expected_conditions模块 提供的预期条件判断类【模块包含一套预定义的条件集合】,大大方便了 WebDriverWait 的使用。

个人博客:https://blog.csdn.net/zyooooxie

一)expected_conditions模块

这儿是官方文档

https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#module-selenium.webdriver.support.expected_conditions

1.实际运用时,一般先通过as关键字将 expected_conditions重命名为ec,再调用预期条件方法来判断;

2.源码中 可以看到很多 条件判断的类 传参是locator 或 element:
locator - used to find the element,locator=(By.Xpath, ‘Xpath’) 使用By方法定位元素的元组;
element 要获取的WebElement元素对象

3.细致说下 17个类的知识( 我呕心沥血的结果 )

不推荐掌握
理由:
(A)实在太多(真心记不住),很多用得机会也很少
(B)有些条件判断的类 正常条件的返回值是A,和显式等待结合后返回值有可能是B,很容易记混
(D)element传参的时候元素的存在(可见、隐藏)与不存在,条件太复杂

        expected_conditions类提供的预期条件 判断方法如下:1.title_is(‘title’) 判断当前页面的title是否完全等于(==)预期字符串,返回布尔值:如果完全一致,返回Ture,否则返回Flase和显式等待结合后,条件符合返回True,不符合 显式等待+报错WebDriverWait(self.driver, 10).until(ec.title_is('百度一下,你就知道'), '失败')    打印结果是True2.title_contains(‘title’) 判断当前页面的title是否包含预期字符串,返回布尔值:True/False;如果完全一致,返回Ture,否则返回Flase和显式等待结合后,条件符合返回True,不符合 显式等待+报错WebDriverWait(self.driver, 10).until(ec.title_contains('知道'), '失败')     打印结果是True3.presence_of_element_located(locator) 判断某个locator元素是否被加到DOM树里,并不代表该元素一定可见(元素是隐藏的)如果定位到就返回WebElement,找不到元素 报错Message: no such element: Unable to locate elementWebDriverWait(driver, 10).until(ec.presence_of_element_located((By.ID, 'su')))      打印结果是WebElement和显式等待结合后,如果定位到就返回WebElement,找不到元素 显式等待+报错4.visibility_of_element_located(locator) 判断某个locator元素是否可见。可见代表非隐藏、可显示,并且元素的宽和高都大于0如果定位到就返回WebElement,找不到元素 报错Message: no such element: Unable to locate elementWebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.ID, 'su')))    打印结果是WebElement和显式等待结合后,如果定位到就返回WebElement,找不到元素 显式等待+报错5.visibility_of(element) 判断element元素是否可见。Visibility means that the element is not only displayed but also has a height and width that is greater than 0.直接传element;如果可见就返回这个WebElement元素;不可见就返回 False;找不到的 报错的是 no such element: Unable to locate element和显式等待结合后,不可见的 显式等待+报错;可见的返回WebElement;不存在的报错 Message: no such element: Unable to locate elementWebDriverWait(driver, 10).until(ec.visibility_of(driver.find_element(By.ID, 'su')))WebDriverWait(driver, 10).until(ec.visibility_of(driver.find_element_by_link_text('高级搜索123')))如果是第二个 实际不会到显式等待10.invisibility_of_element_located(locator) 判断某个locator元素是否隐藏DOM树(隐藏、不存在、可见)# 可见的元素返回False,不存在的元素见返回True;隐藏的元素返回WebElement# 和显式等待结合后,可见的元素显式等待+报错;隐藏的元素会返回WebElement;不存在的元素 返回True# WebDriverWait(self.driver, 10).until(ec.invisibility_of_element_located((By.ID, 'toStationText')), '失败')7.text_to_be_present_in_element(locator,text) 判断某个locator元素的text是否包含了预期的字符串# 请先确定此元素的text [元素文本]# 符合就返回True,不符合返回False;# 和显式等待结合后,符合返回True;不符合,显式等待+报错WebDriverWait(self.driver, 10).until(ec.text_to_be_present_in_element((By.PARTIAL_LINK_TEXT, '多产'), '产品'), '失败')8.text_to_be_present_in_element_value(locator,value) 判断某个元素的value属性值是否包含了预期的字符串# 请先确定此元素的value属性值 print(abc.get_attribute('value'))# 条件符合 返回True;条件不符合返回False# 和显式等待结合后,返回返回True;条件不符合 显式等待+报错# WebDriverWait(driver, 10).until(ec.text_to_be_present_in_element((By.ID, 'su'),'百度一下'))# 等待'su'元素的value属性值是否包含'百度一下'9.frame_to_be_available_and_switch_to_it() 判断该frame是否可以switch进去# 如果直接传入定位方式id,可以的话就返回True并switch进去,否则返回False# 也可传入locator元组或WebElement,可以的话就返回True并switch进去,否则报错 Message: no such element# 和显式等待结合后,传入定位方式id、locator元组,可以的话就返回True并switch进去,否则 显式等待+报错# 和显式等待结合后,传入WebElement,可以的话就返回True并switch进去,# 否则找不到定位报错 selenium.common.exceptions.NoSuchElementException: Message: no such element# WebElement因为是去定位某个元素,定位不到会报错,用不到那个显式等待的!在执行前就已经报错 找不到元素frame的定位 driver.switch_to.frame(xxxx)   xxxx可以是id属性值;或是查找到这个元素WebElement,不可以使用locator元组WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it('frame_id'))WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it((By.ID, 'frame_id')))WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it(driver.find_element(By.ID, 'frame_id')))11.element_to_be_clickable(locator) 判断某个locator元素是否可点击# 元素可以被看见visibility_of_element_located()并且可以被使用is_enabled()返回WebElement;# 找不到的元素 报错 Message: no such element: Unable to locate element# 隐藏的元素返回 False;# WebDriverWait 实际显式等待+ 报错的 包括 隐藏的元素和不存在的元素;可点击返回WebElement# WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.ID, 'su')))12.staleness_of(element) 等某个元素从DOM树中移除。传入WebElement对象# returns False if the element is still attached to the DOM, true otherwise.# 如果元素is_enabled()返回False;其他返回True;根本不存在的报错 Message: no such element# 和显式等待结合后,元素is_enabled() 显式等待+报错;不然返回True;根本不存在的报错 Message: no such elementWebDriverWait(self.driver, 10).until(ec.staleness_of(self.driver.find_element_by_id('su')))13.element_to_be_selected(element) 等待element元素是被选中,一般用在下拉列表# 如果元素是被选中的,返回True;如果不是,返回False;定位失败的 报错 Message: no such element: Unable to locate element# 和显式等待结合后,符合条件返回True;不符合就显式等待+报错;定位失败的 报错 Message: no such element16.element_located_to_be_selected(locator) 等待locator元素是被选中# 如果元素是被选中的,返回True;如果不是,返回False;元素是根本不存在的,就报错 Message: no such element: Unable to locate element# 和显式等待结合后,符合条件的选项返回True;不符合条件的选项、和不存在的选项 都显式等待+报错14.element_selection_state_to_be(element, is_selected) 判断某个元素的选中状态是否符合预期;# 传入WebElement对象以及2种状态(True、False),相等返回True,否则返回False;找不到的报错 Message: no such element: Unable to locate element# is_selected is a Boolean.所以状态一定不能输入'is_selected'# 和显式等待结合后 符合判断条件的返回True;不符合判断条件的 显式等待+ 报错;找不到元素的报错 Message: no such element: Unable to locate element15.element_located_selection_state_to_be(locator,is_selected) 判断某个元素的选中状态是否符合预期。# 传入locator以及2种状态(True、False),相等返回True,否则返回False;找不到的报错 Message: no such element: Unable to locate element# is_selected is a Boolean.所以状态一定 不能输入'is_selected'# print(ec.element_located_selection_state_to_be(abcd10, 'is_selected')(self.driver))  # 返回 False# 和显式等待结合后,符合状态判断条件的选项 返回True;实际 显式等待 + 报错的 包括不符合状态判断条件的选项 和不存在的选项17.alert_is_present 判断页面是否存在alert# 如果有就切换到alert并返回这个Alert;如果没有就返回False# 和显式等待结合后,没有alert 就显式等待+报错;有alert 就返回这个Alert6.presence_of_all_elements_located(locator) 判断是否至少有1个元素存在DOM树中。# 如果定位到就返回WebElement列表,不存在的元素 返回的是 空列表;# 和显式等待结合后,定位到就返回WebElement列表,查不到这些元素 显式等待 + 报错# 和显式等待结合后, 符合 最少存在一个WebElement的 返回符合定位元素条件WebElement的列表,找不到元素的 显式等待+报错WebDriverWait(driver, 10).until(ec.presence_of_all_elements_located((By.ID, 'su')))18.visibility_of_any_elements_located 判断页面至少有一个元素可见 visible,传入locator,一旦定位就返回 the list of located WebElements;不可见(元素隐藏 或是 完全不存在,一个都没有)返回的是 空列表;和显式等待结合后, 符合 最少存在一个WebElement的 返回符合定位元素条件WebElement的列表,不可见(元素隐藏 或是 完全不存在的)显式等待+报错19.visibility_of_all_elements_located 判断页面all elements存在且可见 visibleall elements are present and visible;传入locator,全部符合的 就返回 the list of located and visible WebElements;不能全部符合的返回False;不存在的元素返回 空列表;和显式等待结合后,符合 全部可见WebElement的 返回符合定位元素条件WebElement的列表,找不到元素的 + WebElement不能全部可见的 显式等待+报错

二)visibility_of_element_located(locator)

上面写的估计没有人会去看。所以分享点实际的:把元素查找作为条件的 显式等待 应该如何写?

expected_conditions模块 这儿有三种关于检查元素存在的类,我就不加上elements、invisible的那几个类了。

class presence_of_element_located(object):""" An expectation for checking that an element is present on the DOMof a page. This does not necessarily mean that the element is visible.locator - used to find the elementreturns the WebElement once it is located"""def __init__(self, locator):self.locator = locatordef __call__(self, driver):return _find_element(driver, self.locator)
class visibility_of_element_located(object):""" An expectation for checking that an element is present on the DOM of apage and visible. Visibility means that the element is not only displayedbut also has a height and width that is greater than 0.locator - used to find the elementreturns the WebElement once it is located and visible"""def __init__(self, locator):self.locator = locatordef __call__(self, driver):try:return _element_if_visible(_find_element(driver, self.locator))except StaleElementReferenceException:return False
class visibility_of(object):""" An expectation for checking that an element, known to be present on theDOM of a page, is visible. Visibility means that the element is not onlydisplayed but also has a height and width that is greater than 0.element is the WebElementreturns the (same) WebElement once it is visible"""def __init__(self, element):self.element = elementdef __call__(self, ignored):return _element_if_visible(self.element)

visibility_of()这个类之前分享过。 expected_conditions模块下的visibility_of()

下面来说下presence_of_element_located() 和 visibility_of_element_located():

(A).元素可见与否
presence_of_element_located()的注释:
This does not necessarily mean that the element is visible 这并不一定意味着该元素是可见的

visibility_of_element_located()的注释:
Visibility means that the element is not only displayed but also has a height and width that is greater than 0 可见性意味着元素不仅会显示,而且高度和宽度也会大于0

(B).传参及返回值
传参都是locator - used to find the element;返回的是the WebElement(略有不同)

UI的自动化测试的前提是 用户看到这个元素,对吧?所以我个人推荐使用的是visibility_of_element_located()

    def test_57j(self):"""expected_conditions模块visibility_of_element_located类"""# visibility_of_element_located(locator) 判断某个locator元素是否可见(前提是存在)# 可见代表非隐藏、可显示,并且元素的宽和高都大于0# 如果定位到就返回WebElementfrom selenium.webdriver.support import expected_conditions as ecfrom selenium.webdriver.support.wait import WebDriverWaitself.driver = webdriver.Chrome()self.driver.maximize_window()self.driver.get("https://www.baidu.com")print('开始', time.ctime())# 传入driver 返回一个WebElementprint(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw'))(self.driver))# 传入driver 返回一个WebElement 获取tag_name\\获取class属性值print(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw'))(self.driver).get_attribute('class'))print('1', time.ctime())try:print(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw111'))(self.driver))except BaseException as e111:print(e111)     # 不存在的元素 报错 Message: no such element: Unable to locate elementprint('2', time.ctime())# 和显式等待结合print(WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw')), '失败'))     # 返回WebElementprint(WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw')), '失败').get_attribute('class'))print('3', time.ctime())try:WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kkk1111')), '失败')except BaseException as e222:print(e222)     # 不存在的元素,显式等待+报错print('4', time.ctime())time.sleep(1)self.driver.quit()

下面是这个用例的执行结果:

开始 Wed Nov 28 12:55:59 2018
<selenium.webdriver.remote.webelement.WebElement (session=“56c7bc254293131e99ba4e78a5edf6ea”, element=“0.7407602759779224-1”)>
s_ipt
1 Wed Nov 28 12:55:59 2018
Message: no such element: Unable to locate element: {“method”:“css selector”,“selector”:“input#kw111”}
(Session info: chrome=67.0.3396.99)
(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.17134 x86_64)

2 Wed Nov 28 12:55:59 2018
<selenium.webdriver.remote.webelement.WebElement (session=“56c7bc254293131e99ba4e78a5edf6ea”, element=“0.7407602759779224-1”)>
s_ipt
3 Wed Nov 28 12:55:59 2018
Message: 失败

4 Wed Nov 28 12:56:10 2018

Ran 1 test in 20.925s

下一次分享 显式等待(三)显式等待在脚本中的实际运用

交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie

1128UI自动化测试经验分享-显式等待(二)expected_conditions模块、visibility_of_element_located(locator)相关推荐

  1. 大恶人吉日嘎拉之走火入魔闭门造车之.NET疯狂架构经验分享系列之(二)后台服务代码部分

    程序写太长了,大家看着也累,我也写着也很辛苦,接下来,还是写得简短一些,尽量多一些截图,少一些文字吧. 同样是,欢迎指点批评的同学,我虚心学习提高,改改以往的高姿态. 架设软件系统就像大家看饭店厨师炒 ...

  2. 大恶人吉日嘎拉之走火入魔闭门造车之.NET疯狂架构经验分享系列之(二)后台服务代码部分...

    程序写太长了,大家看着也累,我也写着也很辛苦,接下来,还是写得简短一些,尽量多一些截图,少一些文字吧. 同样是,欢迎指点批评的同学,我虚心学习提高,改改以往的高姿态. 架设软件系统就像大家看饭店厨师炒 ...

  3. selenium-隐式等待和显式等待-0223

    页面等待 隐式等待 driver.implicitly_wait(时间) 网页打开以后会等待指定的时间,然后才会继续往下查找相关的一些标签 显式等待 演练例子要加载一个Id并不存在的标签,如果加载不成 ...

  4. 三种等待方式:强制等待、显式等待、隐式等待

    我们在使用selenium的时候,会遇到一种定位不到的情况,因为web页面有一个加载的过程 当页面元素未出现时,去定位肯定是定位不到的,所以我们需要用到了'等待',该如何使用等待呢,让我们一起来探讨一 ...

  5. 【selenium-python】显式等待和隐式等待的使用和区别

    我的博客 网上教程挺多,看完还是没太理解,看了官方文档稍微理解了一些,在此记录. 部分观点为个人理解,请批判性阅读.如有错误,请指正,万分感谢. 参考: webdriver_waits When to ...

  6. 5-Selenium WebDriver三种等待--隐式等待-显式等待和流畅等待

    在selenium中,Waits在执行测试中扮演重要角色.在本教程中,您将学习Selenium中"隐式"和"显式"等待的各个方面. 在Selenium中为什么需 ...

  7. Selenium中的隐式等待和显式等待

    在Selenium中,"等待"在执行测试中起着重要作用.在本文中,您将学习Selenium中"隐式"和"显式"等待的各个方面. 在本文中,您 ...

  8. python selenium 显式等待和隐式等待

    不同点: 1.隐式等待式全局性的,针对素有的查找元素.显式等待是局部的,只是针对一个或一组元素的查找. 2.隐式等待可以设置查找条件. 相同点: 1.都是智能等待,都需要设置最长等待时间,在最长等待时 ...

  9. Python网络爬虫Selenium页面等待:强制等待、隐式等待和显式等待

    关于Python网络爬虫Selenium强制等待.隐式等待和显式等待的总结 from selenium import webdriver import time from selenium.webdr ...

最新文章

  1. 20-flutter下拉刷新与上拉加载
  2. 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程
  3. 可以分屏吗_LED透明屏分屏是怎么一回事?
  4. [转]jQuery 读取 xml
  5. sublime text里添加对Gradle配置文件的支持
  6. 在Java中使用Collat​​or和String类进行字符串比较
  7. 超级vga显示卡_VGA视频采集卡常见故障分析
  8. vivo正式公布全新子品牌iQOO手机:冲击5千元+旗舰手机档
  9. tm图像融合编程matlab,遥感习题
  10. 中国内窥镜干燥存放柜市场趋势报告、技术动态创新及市场预测
  11. vue项目中的回车登录
  12. 问题集录--新手入门深度学习,选择TensorFlow 好吗?
  13. SAPUI5入门到精通5---MVC和数据绑定
  14. android addr2line 用法,分析安卓ANR tombstone使用ndk-stack addr2line
  15. Android通知栏图标显示网络图片
  16. 一部《再忆王家沱》讲述百年重庆历史,堪称中国版《百年孤独》
  17. 【实践】haskell、coq基本语法
  18. http上传文件流程 使用winlnet
  19. 如何使错误日志更加方便排查问题?
  20. C#学习之面象对象继承练习(二)

热门文章

  1. 微信公众号网页授权多域名解决方案
  2. 在群晖上搭建基于 PostgreSQL 的 Joplin Server
  3. c语言浮点变量是什么意思,C语言中说的浮点型是什么意思呢 C语言的浮点数是什么...
  4. 云南计算机网络技术排名,2017年云南大学排名
  5. 视频字幕识别(百度AI开放平台OCR | python | opencv)
  6. springboot整合jett导出数据(2)
  7. python获取列表中某个元素个数_如何获取列表中的元素数?
  8. 推荐一款我最近爱上的网页版文库(编辑器)——语雀yuque.com
  9. NSIS脚本学习:使用 LogicLib.nsh 实现基本流程控制结构
  10. 睿智的目标检测32——TF2搭建YoloV4目标检测平台(tensorflow2)