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操作MongoDB基础知识
Nov 01 Python
python获取文件扩展名的方法
Jul 06 Python
Python中多个数组行合并及列合并的方法总结
Apr 12 Python
python 基本数据类型占用内存空间大小的实例
Jun 12 Python
python 与服务器的共享文件夹交互方法
Dec 27 Python
Python操作SQLite数据库过程解析
Sep 02 Python
python 实现return返回多个值
Nov 19 Python
python中的线程threading.Thread()使用详解
Dec 17 Python
Python连接Hadoop数据中遇到的各种坑(汇总)
Apr 14 Python
scrapy与selenium结合爬取数据(爬取动态网站)的示例代码
Sep 28 Python
Django Admin后台模型列表页面如何添加自定义操作按钮
Nov 11 Python
python绘图subplots函数使用模板的示例代码
Apr 30 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
阿拉伯的咖啡与水烟
2021/03/03 咖啡文化
有关phpmailer的详细介绍及使用方法
2013/01/28 PHP
利用php+mcDropdown实现文件路径可在下拉框选择
2013/08/07 PHP
Array.prototype 的泛型应用分析
2010/04/30 Javascript
JS/jQuery实现默认显示部分文字点击按钮显示全部内容
2013/05/13 Javascript
七个不允许错过的jQuery小技巧
2015/12/21 Javascript
全面了解js中的script标签
2016/07/04 Javascript
AngularJS基础 ng-model 指令详解及示例代码
2016/08/02 Javascript
js仿腾讯QQ的web登陆界面
2016/08/19 Javascript
Javascript将字符串日期格式化为yyyy-mm-dd的方法
2016/10/27 Javascript
Spring Boot+AngularJS+BootStrap实现进度条示例代码
2017/03/02 Javascript
详解Vue-cli 创建的项目如何跨域请求
2017/05/18 Javascript
vue父组件向子组件(props)传递数据的方法
2018/01/02 Javascript
详解Vue基于 Nuxt.js 实现服务端渲染(SSR)
2018/04/05 Javascript
React Native日期时间选择组件的示例代码
2018/04/27 Javascript
JS获取浏览器地址栏的多个参数值的任意值实例代码
2018/07/24 Javascript
原生JS无缝滑动轮播图
2019/10/22 Javascript
关于vue2强制刷新,解决页面不会重新渲染的问题
2019/10/29 Javascript
JavaScript鼠标拖拽事件详解
2020/04/03 Javascript
JQuery事件冒泡和默认行为代码实例
2020/05/13 jQuery
手把手带你搭建一个node cli的方法示例
2020/08/07 Javascript
python字符串连接方式汇总
2014/08/21 Python
pyenv命令管理多个Python版本
2017/03/26 Python
Python基于tkinter模块实现的改名小工具示例
2017/07/27 Python
Python切片索引用法示例
2018/05/15 Python
python双端队列原理、实现与使用方法分析
2019/11/27 Python
Jupyter notebook命令和编辑模式常用快捷键汇总
2020/11/17 Python
美国高端寝具品牌:Coyuchi
2017/02/08 全球购物
澳大利亚拥有最佳跳伞降落点和最好服务的跳伞项目运营商:Skydive Australia
2018/03/05 全球购物
工作自荐信
2013/12/11 职场文书
《三袋麦子》教学反思
2014/03/02 职场文书
民间个人借款协议书
2014/09/30 职场文书
机关党员四风问题个人整改措施
2014/10/26 职场文书
党风廉正建设责任书
2015/01/29 职场文书
2015年初三班主任工作总结
2015/05/21 职场文书
CSS中使用grid布局实现一套模板多种布局
2022/07/15 HTML / CSS