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实现划词翻译
Apr 23 Python
Python获取SQLite查询结果表列名的方法
Jun 21 Python
详解Python文本操作相关模块
Jun 22 Python
在Python中执行系统命令的方法示例详解
Sep 14 Python
itchat接口使用示例
Oct 23 Python
给你选择Python语言实现机器学习算法的三大理由
Nov 15 Python
python之消除前缀重命名的方法
Oct 21 Python
python图形界面开发之wxPython树控件使用方法详解
Feb 24 Python
django修改models重建数据库的操作
Mar 31 Python
快速解决pymongo操作mongodb的时区问题
Dec 05 Python
python中PyQuery库用法分享
Jan 15 Python
Python基本知识点总结
Apr 07 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
对Session和Cookie的区分与解释
2007/03/16 PHP
利用PHP制作简单的内容采集器的代码
2007/11/28 PHP
PHP 防恶意刷新实现代码
2010/05/16 PHP
PHP JSON格式数据交互实例代码详解
2011/01/13 PHP
深入php list()函数的详解
2013/06/05 PHP
Yii2隐藏frontend/web和backend/web的方法
2015/12/12 PHP
php屏蔽错误及提示的方法
2020/05/10 PHP
PHP网页缓存技术优点及代码实例
2020/07/29 PHP
CheckBox 如何实现全选?
2006/06/23 Javascript
javascript 获取元素位置的快速方法 getBoundingClientRect()
2009/11/26 Javascript
Jquery带搜索框的下拉菜单
2013/05/06 Javascript
jQuery实现html表格动态添加新行的方法
2015/05/28 Javascript
JS组件中bootstrap multiselect两大组件较量
2016/01/26 Javascript
原生js实现可爱糖果数字时间特效
2016/12/30 Javascript
canvas绘制的直线动画
2017/01/23 Javascript
ES6新特性之数组、Math和扩展操作符用法示例
2017/04/01 Javascript
Vue.js实战之组件之间的数据传递
2017/04/01 Javascript
详谈angularjs中路由页面强制更新的问题
2017/04/24 Javascript
AngularJS中的路由使用及实现代码
2017/10/09 Javascript
nodejs判断文件、文件夹是否存在及删除的方法
2017/11/10 NodeJs
js中bool值的转换及“&&”、“||”、 “!!”详解
2017/12/21 Javascript
nodejs+mongodb+vue前后台配置ueditor的示例代码
2018/01/02 NodeJs
修改node.js默认的npm安装目录实例
2018/05/15 Javascript
原生js实现随机点餐效果
2019/12/10 Javascript
Python编程实现粒子群算法(PSO)详解
2017/11/13 Python
python 猴子补丁(monkey patch)
2019/06/26 Python
Flask框架 CSRF 保护实现方法详解
2019/10/30 Python
Pycharm debug调试时带参数过程解析
2020/02/03 Python
Canvas 文字碰撞检测并抽稀的方法
2019/05/27 HTML / CSS
VICHY薇姿英国官网:全球专业敏感肌护肤领先品牌
2017/07/04 全球购物
服装设计专业自荐书范文
2013/12/30 职场文书
房地产财务管理制度
2014/02/02 职场文书
小学教师师德整改措施
2014/09/29 职场文书
六一亲子活动感想
2015/08/07 职场文书
超详细教你怎么升级Mysql的版本
2021/05/19 MySQL
python 中的jieba分词库
2021/11/23 Python