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里对list中的整数求平均并排序
Sep 12 Python
python中pygame针对游戏窗口的显示方法实例分析(附源码)
Nov 11 Python
linux环境下的python安装过程图解(含setuptools)
Nov 22 Python
python通过百度地图API获取某地址的经纬度详解
Jan 28 Python
教你使用python实现微信每天给女朋友说晚安
Mar 23 Python
python看某个模块的版本方法
Oct 16 Python
Python安装与基本数据类型教程详解
May 29 Python
python中的itertools的使用详解
Jan 13 Python
python 中不同包 类 方法 之间的调用详解
Mar 09 Python
PyQt5 控件字体样式等设置的实现
May 13 Python
python数据类型强制转换实例详解
Jun 22 Python
Python 文字识别
May 11 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
如何使用微信公众平台开发模式实现多客服
2016/01/06 PHP
非常实用的php验证码类
2016/05/15 PHP
CI框架数据库查询之join用法分析
2016/05/18 PHP
利用javascript查看html源文件
2006/11/08 Javascript
Javascript的并行运算实现代码
2010/11/19 Javascript
Node.js模拟浏览器文件上传示例
2014/03/26 Javascript
JavaScript之数组(Array)详解
2015/04/01 Javascript
jquery判断复选框是否被选中的方法
2015/10/16 Javascript
Javascript将字符串日期格式化为yyyy-mm-dd的方法
2016/10/27 Javascript
JavaScript实现经纬度转换成地址功能
2017/03/28 Javascript
jQuery异步提交表单实例
2017/05/30 jQuery
vue使用xe-utils函数库的具体方法
2018/03/06 Javascript
快速解决angularJS中用post方法时后台拿不到值的问题
2018/08/14 Javascript
解决vue无法设置滚动位置的问题
2018/10/07 Javascript
js实现通过开始结束控制的计时器
2019/02/25 Javascript
vue组件定义,全局、局部组件,配合模板及动态组件功能示例
2019/03/19 Javascript
Node.JS发送http请求批量检查文件中的网页地址、服务是否有效可用
2019/11/20 Javascript
JS脚本实现定时到网站上签到/签退功能
2020/04/22 Javascript
vue根据条件不同显示不同按钮的操作
2020/08/04 Javascript
[03:31]DOTA2英雄基础教程 大地之灵
2013/12/17 DOTA
详解Python中映射类型的内建函数和工厂函数
2015/08/19 Python
深入浅出学习python装饰器
2017/09/29 Python
python实现单链表中删除倒数第K个节点的方法
2018/09/28 Python
Pandas之MultiIndex对象的示例详解
2019/06/25 Python
在python 中split()使用多符号分割的例子
2019/07/15 Python
Python通过递归获取目录下指定文件代码实例
2019/11/07 Python
python3.7+selenium模拟淘宝登录功能的实现
2020/05/26 Python
SmartBuyGlasses美国官网:太阳眼镜和眼镜
2017/08/20 全球购物
Linux文件系统类型
2012/02/15 面试题
互联网创业计划书的书写步骤
2014/01/28 职场文书
高考励志标语
2014/06/05 职场文书
药店促销活动策划方案
2014/08/24 职场文书
2016年小学“感恩教师”主题队日活动总结
2016/04/01 职场文书
详解Nginx启动失败的几种错误处理
2021/04/01 Servers
golang json数组拼接的实例
2021/04/28 Golang
Nginx禁止ip访问或非法域名访问
2022/04/07 Servers