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实现向服务器请求压缩数据及解压缩数据的方法示例
Jun 09 Python
python3大文件解压和基本操作
Dec 15 Python
python利用requests库进行接口测试的方法详解
Jul 06 Python
详解Python中的分组函数groupby和itertools)
Jul 11 Python
对python多线程中互斥锁Threading.Lock的简单应用详解
Jan 11 Python
Python简直是万能的,这5大主要用途你一定要知道!(推荐)
Apr 03 Python
PyQt5实现从主窗口打开子窗口的方法
Jun 19 Python
Django分页功能的实现代码详解
Jul 29 Python
40行Python代码实现天气预报和每日鸡汤推送功能
Feb 27 Python
python matplotlib包图像配色方案分享
Mar 14 Python
python3注册全局热键的实现
Mar 22 Python
python等待10秒执行下一命令的方法
Jul 19 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
常用表单验证类,有了这个,一般的验证就都齐了。
2006/12/06 PHP
php防注
2007/01/15 PHP
PHP缓存集成库phpFastCache用法
2014/12/15 PHP
PHP Imagick完美实现图片裁切、生成缩略图、添加水印
2016/02/22 PHP
php技巧小结【推荐】
2017/01/19 PHP
Yii框架连表查询操作示例
2019/09/06 PHP
extJs 下拉框联动实现代码
2010/04/09 Javascript
javascript setTimeout和setInterval计时的区别详解
2013/06/21 Javascript
jQuery结合HTML5制作的爱心树表白动画
2015/02/01 Javascript
JS实现仿google、百度搜索框输入信息智能提示的实现方法
2015/04/20 Javascript
js简单实现竖向tab选项卡的方法
2015/05/04 Javascript
jquery实现兼容IE8的异步上传文件
2015/06/15 Javascript
JQuery异步加载PartialView的方法
2016/06/07 Javascript
利用js定义一个导航条菜单
2017/03/14 Javascript
jquery平滑滚动到顶部插件使用详解
2017/05/08 jQuery
详解AngularJS ng-class样式切换
2017/06/27 Javascript
Node.js利用js-xlsx处理Excel文件的方法详解
2017/07/05 Javascript
express如何使用session与cookie的方法
2018/01/30 Javascript
JS插件clipboard.js实现一键复制粘贴功能
2020/12/04 Javascript
微信小程序实现卡片层叠滑动效果
2019/06/21 Javascript
vant中的toast轻提示实现代码
2020/11/04 Javascript
[38:27]完美世界DOTA2联赛PWL S2 Forest vs FTD.C 第二场 11.26
2020/11/30 DOTA
python使用新浪微博api上传图片到微博示例
2014/01/10 Python
Python减少循环层次和缩进的技巧分析
2016/03/15 Python
TensorFlow损失函数专题详解
2018/04/26 Python
Python内置random模块生成随机数的方法
2019/05/31 Python
python使用装饰器作日志处理的方法
2019/07/11 Python
Html5移动端div固定到底部实现底部导航条的几种方式
2021/03/09 HTML / CSS
六查六看剖析材料
2014/02/15 职场文书
乡镇安全生产目标责任书
2014/07/23 职场文书
论文答谢词
2015/01/20 职场文书
通知怎么写?
2019/04/17 职场文书
感谢信的技巧及范例
2019/05/15 职场文书
《中华上下五千年》读后感3篇
2019/11/29 职场文书
python控制台打印log输出重复的解决方法
2021/05/14 Python
Python学习开发之图形用户界面详解
2021/08/23 Python