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 文件和路径操作函数小结
Nov 23 Python
python获取远程图片大小和尺寸的方法
Mar 26 Python
Python中super关键字用法实例分析
May 28 Python
简介二分查找算法与相关的Python实现示例
Aug 26 Python
python操作oracle的完整教程分享
Jan 30 Python
解决python给列表里添加字典时被最后一个覆盖的问题
Jan 21 Python
python3利用ctypes传入一个字符串类型的列表方法
Feb 12 Python
python协程gevent案例 爬取斗鱼图片过程解析
Aug 27 Python
Python 根据数据模板创建shapefile的实现
Nov 26 Python
Python如何基于rsa模块实现非对称加密与解密
Jan 03 Python
python使用openpyxl操作excel的方法步骤
May 28 Python
python中pandas对多列进行分组统计的实现
Jun 18 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教程孙仲岳主讲
2008/01/07 PHP
php中替换字符串中的空格为逗号','的方法
2014/06/09 PHP
PHP管理依赖(dependency)关系工具 Composer 安装与使用
2014/08/18 PHP
Laravel 5框架学习之向视图传送数据(进阶篇)
2015/04/08 PHP
PHP实现类似题库抽题效果
2018/08/16 PHP
jsp+javascript打造级连菜单的实例代码
2013/06/14 Javascript
jQuery ajax dataType值为text json探索分享
2013/09/23 Javascript
jQuery中parents()方法用法实例
2015/01/07 Javascript
ztree获取当前选中节点子节点id集合的方法
2015/02/12 Javascript
javascript实现网页屏蔽Backspace事件,输入框不屏蔽
2015/07/21 Javascript
简述jQuery ajax的执行顺序
2016/01/05 Javascript
JS组件Bootstrap dropdown组件扩展hover事件
2016/04/17 Javascript
JavaScript实现Base64编码转换
2016/04/23 Javascript
node.js入门学习之url模块
2017/02/25 Javascript
利用jQuery实现一个简单的表格上下翻页效果
2017/03/14 Javascript
Vue路由切换时的左滑和右滑效果示例
2018/05/29 Javascript
AngularJs返回前一页面时刷新一次前面页面的方法
2018/10/09 Javascript
分享8个JavaScript库可更好地处理本地存储
2020/10/12 Javascript
vue使用swiper实现左右滑动切换图片
2020/10/16 Javascript
用实例分析Python中method的参数传递过程
2015/04/02 Python
使用SAE部署Python运行环境的教程
2015/05/05 Python
对python 通过ssh访问数据库的实例详解
2019/02/19 Python
Python分割训练集和测试集的方法示例
2019/09/19 Python
解决Jupyter Notebook使用parser.parse_args出现错误问题
2020/04/20 Python
CSS3 display知识详解
2015/11/25 HTML / CSS
北美个性化礼品商店:Things Remembered
2018/06/12 全球购物
美国价格实惠的在线眼镜网站:Zeelool
2020/12/25 全球购物
前台接待的工作职责
2013/11/21 职场文书
陈欧广告词
2014/03/14 职场文书
大型演出策划方案
2014/05/28 职场文书
新学期标语
2014/06/30 职场文书
离婚起诉书范本
2015/05/18 职场文书
Feign调用传输文件异常的解决
2021/06/24 Java/Android
Java并发编程必备之Future机制
2021/06/30 Java/Android
数据库之SQL技巧整理案例
2021/07/07 SQL Server
Python日志模块logging用法
2022/06/05 Python