Expected conditions模块使用方法汇总代码解析


Posted in Python onAugust 13, 2020

一、expected_conditions模块是什么?

是Selenium的一个子模块,selenium.webdriver.support.expected_conditions

可以对网页上元素是否存在,可点击等等进行判断,一般用于断言或与WebDriverWait配合使用

二、expected_conditions模块简单应用

2.1 WebDriverWait与expected_conditions配合使用实例一

import os
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')

# 等待10s,等待过程中判断网页标题是否是"百度一下,你就知道"
# 如果是就继续执行后续代码,反之等待10s结束时报错
WebDriverWait(driver,10).until(EC.title_is("百度一下,你就知道"))

2.2 WebDriverWait与expected_conditions配合使用实例二

from selenium import webdriver
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.get('https://www.baidu.com')
#等待10s,等待过程中如果定位到元素,就直接执行后续的代码,反之等待10s后报错误信息
element = WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(By.ID,'kw')))
element.send_keys( '新梦想软件测试' )

2.3 unittest与expected_conditions配合使用实例

import time
import unittest
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

class TestDemo(unittest.TestCase):
  def setUp(self) :
    self.driver = webdriver.Chrome()
  def tearDown(self):
    time.sleep(2)
    self.driver.quit()

  def test_searchinputbox_is_visibility(self):
    self.driver.get('https://www.baidu.com')
    #EC.visibility_of()判断元素是否可见,如果可见就返回这个元素 
    self.assertTrue(EC.visibility_of(self.driver.find_element(By.ID,'kw')))
if __name__=='__main__':
  unittest.main()

实例小结:

实例一与实例二中用到了显式等待 WebDriverWait类,该块不在此文中介绍;

实例三中self.assertTrue()方法断言括号内的表达式返回值是否为ture,在python中代表true的为 非0、非空、true,而

EC.visibility_of()方法中的定位方法能定位到元素就会返回一个对象,满足非空为true,所以断言会通过;

注意EC.visibility_of()方法返回的对象非真实元素对象,所以不能执行如下代码:(正确方式参照实例二的写法)

element = EC.visibility_of(self.driver.find_element(By.ID,'kw'))
element.send_keys('newdream')

三、expected_conditions模块用法汇总

#判断当前页面的title是否精确等于预期,返回布尔值
WebDriverWait(driver,10).until(EC.title_is("百度一下,你就知道"))
#判断当前页面的title是否包含预期字符串,返回布尔值
WebDriverWait(driver,10).until(EC.title_contains('new'))
#判断当前页面的url是否精确等于预期,返回布尔值
WebDriverWait(driver,10).until(EC.url_contains('https://www.baidu.com'))
#判断当前页面的url是否包含预期字符串,返回布尔值
WebDriverWait(driver,10).until(EC.url_contains('baidu'))
#判断当前页面的url是否满足字符串正则表达式匹配,返回布尔值
WebDriverWait(driver,10).until(EC.url_matches('.+baidu.+'))
#判断元素是否出现,只要有一个元素出现,返回元素对象
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw')))
#判断元素是否可见,返回元素对象
WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(By.ID,'kw')))
#判断元素是否包含指定文本,返回布尔值
WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.NAME,'tj_trnews'),'新闻'))
#判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去
WebDriverWait(driver,10,).until(EC.frame_to_be_available_and_switch_to_it(By.xpath,'//iframe'))
#判断某个元素是否可见并且是可点击的,如果是的就返回这个元素,否则返回False
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,'tj_trnews')))
#判断某个元素是否被选中,一般用在下拉列表
WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.xpath,'//input[@type="checkbox"]')))
#判断页面上是否存在alert,如果有就切换到alert并返回alert的内容
WebDriverWait(driver,10).until(EC.alert_is_present())

备注:以上整理大家要注意参数和返回值,部分参数是元素对象,部分是locator的元组,如(By.NAME,'tj_trnews')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python通过floor函数舍弃小数位的方法
Mar 17 Python
Django实现简单分页功能的方法详解
Dec 05 Python
Python文本处理之按行处理大文件的方法
Apr 09 Python
python生成ppt的方法
Jun 07 Python
python针对不定分隔符切割提取字符串的方法
Oct 26 Python
Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法
Apr 18 Python
pandas 数据结构之Series的使用方法
Jun 21 Python
python 批量解压压缩文件的实例代码
Jun 27 Python
python交易记录链的实现过程详解
Jul 03 Python
使用python 将图片复制到系统剪贴中
Dec 13 Python
Selenium使用Chrome模拟手机浏览器方法解析
Apr 10 Python
pycharm激活码免费分享适用最新pycharm2020.2.3永久激活
Nov 25 Python
深入了解Python装饰器的高级用法
Aug 13 #Python
python高级特性简介
Aug 13 #Python
Pytest如何使用skip跳过执行测试
Aug 13 #Python
matplotlib基础绘图命令之bar的使用方法
Aug 13 #Python
Python logging模块原理解析及应用
Aug 13 #Python
matplotlib基础绘图命令之imshow的使用
Aug 13 #Python
使用jupyter notebook运行python和R的步骤
Aug 13 #Python
You might like
PHP网上调查系统
2006/10/09 PHP
ThinkPHP整合百度Ueditor图文教程
2014/10/21 PHP
实例讲解PHP中使用命名空间
2019/01/27 PHP
PHP连接SQL server数据库测试脚本运行实例
2020/08/24 PHP
jQuery 注意事项 与原因分析
2009/04/24 Javascript
Jquery 一次处理多个ajax请求的代码
2011/09/02 Javascript
不使用浏览器运行javascript代码的方法
2013/07/24 Javascript
js中function()使用方法
2013/12/24 Javascript
js对象内部访问this修饰的成员函数示例
2014/04/27 Javascript
js随机生成字母数字组合的字符串 随机动画数字
2015/09/02 Javascript
JavaScript实现复制内容到粘贴板代码
2016/03/31 Javascript
JavaScript地理位置信息API
2016/06/11 Javascript
浅谈Angular4实现热加载开发旅程
2017/09/08 Javascript
Javascript网页抢红包外挂实现分享
2018/01/11 Javascript
jquery的 filter()方法使用教程
2018/03/22 jQuery
Vuex实现数据共享的方法
2019/12/20 Javascript
JavaScript中reduce()的5个基本用法示例
2020/07/19 Javascript
js节流防抖应用场景,以及在vue中节流防抖的具体实现操作
2020/09/21 Javascript
使用原生javascript开发计算器实例代码
2021/02/21 Javascript
[02:34]DOTA2英雄基础教程 幽鬼
2014/01/02 DOTA
python3实现zabbix告警推送钉钉的示例
2019/02/20 Python
利用Python实现kNN算法的代码
2019/08/16 Python
详解如何用python实现一个简单下载器的服务端和客户端
2019/10/28 Python
django在保存图像的同时压缩图像示例代码详解
2020/02/11 Python
python线程join方法原理解析
2020/02/11 Python
jupyter notebook 参数传递给shell命令行实例
2020/04/10 Python
在css3中background-clip属性与background-origin属性的用法介绍
2012/11/13 HTML / CSS
Under Armour澳大利亚官网:美国知名的高端功能性运动品牌
2018/02/22 全球购物
药品质量检测应届生求职信
2013/11/14 职场文书
人力资源专业推荐信
2013/11/29 职场文书
2014年教育工作总结
2014/11/26 职场文书
世界文化遗产导游词
2015/02/13 职场文书
党员承诺书范文2015
2015/04/27 职场文书
24句精辟的现实社会语录,句句扎心,道尽人性
2019/08/29 职场文书
一篇文章弄懂MySQL查询语句的执行过程
2021/05/07 MySQL
Python线程池与GIL全局锁实现抽奖小案例
2022/04/13 Python