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中的pydoc模块和distutils模块
Apr 13 Python
python编码总结(编码类型、格式、转码)
Jul 01 Python
python中执行shell的两种方法总结
Jan 10 Python
python实现简易动态时钟
Nov 19 Python
使用Python开发SQLite代理服务器的方法
Dec 07 Python
Python2与Python3的区别实例分析
Apr 11 Python
python+numpy实现的基本矩阵操作示例
Jul 19 Python
python图形开发GUI库pyqt5的基本使用方法详解
Feb 14 Python
520使用Python实现“我爱你”表白
May 20 Python
Python3与fastdfs分布式文件系统如何实现交互
Jun 23 Python
python实现sm2和sm4国密(国家商用密码)算法的示例
Sep 26 Python
如何基于Python实现word文档重新排版
Sep 29 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实现异步调用方法研究与分享
2011/10/27 PHP
php笔记之:php数组相关函数的使用
2013/04/26 PHP
PHP实现网站应用微信登录功能详解
2019/04/11 PHP
JavaScript入门之对象与JSON详解
2011/10/21 Javascript
设为首页加入收藏兼容360/火狐/谷歌/IE等主流浏览器的代码
2013/03/26 Javascript
原始的js代码和jquery对比体会
2013/09/10 Javascript
对Web开发中前端框架与前端类库的一些思考
2015/03/27 Javascript
AngularJS实现树形结构(ztree)菜单示例代码
2016/09/18 Javascript
原生js实现放大镜效果
2017/01/11 Javascript
详解Angular 开发环境搭建
2017/06/22 Javascript
underscore之function_动力节点Java学院整理
2017/07/11 Javascript
JS库之ParticlesJS使用简介
2017/09/12 Javascript
jquery 给动态生成的标签绑定事件的几种方法总结
2018/02/24 jQuery
angularJs中$scope数据序列化的实例
2018/09/30 Javascript
echarts实现词云自定义形状的示例代码
2019/02/20 Javascript
vue使用自定义指令实现拖拽
2021/01/29 Javascript
JavaScript如何判断input数据类型
2020/02/06 Javascript
Vue使用vue-draggable 插件在不同列表之间拖拽功能
2020/03/12 Javascript
[04:19]DOTA2亚洲邀请赛 现场花絮
2015/03/11 DOTA
python分析apache访问日志脚本分享
2015/02/26 Python
Python写的一个定时重跑获取数据库数据
2016/12/28 Python
windows下python之mysqldb模块安装方法
2017/09/07 Python
Python实现XML文件解析的示例代码
2018/02/05 Python
python统计字母、空格、数字等字符个数的实例
2018/06/29 Python
Flask入门之上传文件到服务器的方法示例
2018/07/18 Python
利用Pyhton中的requests包进行网页访问测试的方法
2018/12/26 Python
Python tkinter制作单机五子棋游戏
2020/09/14 Python
HTML5网页音乐播放器的示例代码
2017/11/09 HTML / CSS
一级方程式赛车官方网上商店:F1 Store(支持中文)
2018/01/12 全球购物
Herve Leger官网:标志性绷带连衣裙等
2018/12/26 全球购物
芬兰设计商店美国:Finnish Design Shop US
2019/03/25 全球购物
请假条的格式
2014/04/11 职场文书
群众路线领导对照材料
2014/08/23 职场文书
篮球赛新闻稿
2015/07/17 职场文书
详解JavaScript中的执行上下文及调用堆栈
2021/04/29 Javascript
CSS变量实现主题切换的方法
2021/06/23 HTML / CSS