python selenium expected_conditions使用实例
今天正好虫师问到selenium python binding中support.expected_conditions的用法,顺手总结了一下,希望对大家有所帮助。
场景
Expected Conditions的使用场景有2种
- 直接在断言中使用
- 与WebDriverWait配合使用,动态等待页面上元素出现或者消失
方法注释
先翻译一下这些方法的用法
title_is: 判断当前页面的title是否精确等于预期title_contains: 判断当前页面的title是否包含预期字符串presence_of_element_located: 判断某个元素是否被加到了dom树里,并不代表该元素一定可见visibility_of_element_located: 判断某个元素是否可见.可见代表元素非隐藏,并且元素的宽和高都不等于0visibility_of: 跟上面的方法做一样的事情,只是上面的方法要传入locator,这个方法直接传定位到的element就好了presence_of_all_elements_located: 判断是否至少有1个元素存在于dom树中。举个例子,如果页面上有n个元素的class都是'column-md-3',那么只要有1个元素存在,这个方法就返回Truetext_to_be_present_in_element: 判断某个元素中的text是否包含了预期的字符串text_to_be_present_in_element_value: 判断某个元素中的value属性是否包含了预期的字符串frame_to_be_available_and_switch_to_it: 判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回Falseinvisibility_of_element_located: 判断某个元素中是否不存在于dom树或不可见element_to_be_clickable: 判断某个元素中是否可见并且是enable的,这样的话才叫clickablestaleness_of: 等某个元素从dom树中移除,注意,这个方法也是返回True或Falseelement_to_be_selected: 判断某个元素是否被选中了,一般用在下拉列表element_selection_state_to_be: 判断某个元素的选中状态是否符合预期element_located_selection_state_to_be: 跟上面的方法作用一样,只是上面的方法传入定位到的element,而这个方法传入locatoralert_is_present: 判断页面上是否存在alert,这是个老问题,很多同学会问到
具体的例子
例子的代码在这里,并且是可以运行通过的。
下面的代码演示了一些常见疑问
- 如何等待页面上的某个元素出现,然后再对这个元素进行操作
- 如何在unittest框架中所有的用例都共用1个浏览器实例,然后在全部用例结束后关闭浏览器
expected_conditions.py
#encoding:utf-8
# example of how to use https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/support/expected_conditions.py
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
import unittest
# dr = webdriver.PhantomJS('phantomjs')
dr = webdriver.Firefox()
# dr = webdriver.Chrome()
url = 'http://www.baidu.com'
search_text_field_id = 'kw'
dr.get(url)
class ECExample(unittest.TestCase):
def test_title_is(self):
''' 判断title是否符合预期 '''
title_is_baidu = EC.title_is(u'百度一下,你就知道')
self.assertTrue(title_is_baidu(dr))
def test_titile_contains(self):
''' 判断title是否包含预期字符 '''
title_should_contains_baidu = EC.title_contains(u'百度')
self.assertTrue(title_should_contains_baidu(dr))
def test_presence_of_element_located(self):
''' 判断element是否出现在dom树 '''
locator = (By.ID, search_text_field_id)
search_text_field_should_present = EC.visibility_of_element_located(locator)
''' 动态等待10s,如果10s内element加载完成则继续执行下面的代码,否则抛出异常 '''
WebDriverWait(dr, 10).until(EC.presence_of_element_located(locator))
WebDriverWait(dr, 10).until(EC.visibility_of_element_located(locator))
self.assertTrue(search_text_field_should_present(dr))
def test_visibility_of(self):
search_text_field = dr.find_element_by_id(search_text_field_id)
search_text_field_should_visible = EC.visibility_of(search_text_field)
self.assertTrue(search_text_field_should_visible('yes'))
def test_text_to_be_present_in_element(self):
text_should_present = EC.text_to_be_present_in_element((By.NAME, 'tj_trhao123'), 'hao123')
self.assertTrue(text_should_present(dr))
@classmethod
def tearDownClass(kls):
print 'after all test'
dr.quit()
print 'quit dr'
if __name__ == '__main__':
unittest.main()
分析
以title_is为例
class title_is(object):
"""An expectation for checking the title of a page.
title is the expected title, which must be an exact match
returns True if the title matches, false otherwise."""
def __init__(self, title):
self.title = title
def __call__(self, driver):
return self.title == driver.title
可以看到title_is实际上是1个class,其__call__方法被定义成是返回1个bool值。因此,一般的用法就是
# 实例化
the_instance = title_is('expected')
# 直接在实例上调用__call__
the_instance(dr) #return True or False
python selenium expected_conditions使用实例的更多相关文章
- Python+Selenium 自动化实现实例-实现文件下载
#coding=utf-8 from selenium import webdriver #实例化一个火狐配置文件 fp = webdriver.FirefoxProfile() #设置各项参数,参数 ...
- Python+Selenium 自动化实现实例-Css捕捉元素的几种方法
#coding=utf-8 from selenium import webdriverimport timedriver = webdriver.Chrome()driver.get("h ...
- Python+Selenium 自动化实现实例-打开浏览器模拟进行搜索数据并验证
#导入模块 from selenium import webdriverfrom selenium.webdriver.common.keys import Keys #启动火狐浏览器driver = ...
- Python+Selenium 自动化实现实例-处理分页(pagination)
场景 对分页来说,我们最感兴趣的是下面几个信息 总共有多少页 当前是第几页 是否可以上一页和下一页 代码 下面代码演示如何获取分页总数及当前页数.跳转到指定页数 #coding:utf-8 from ...
- Python+Selenium 自动化实现实例-定位frame中的元素
场景 处理frame需要用到2个方法,分别是switch_to_frame(name_or_id_or_frame_element)和switch_to_default_content() 如何理解这 ...
- Python+Selenium 自动化实现实例-单元测试报告
代码如下: # -*- coding: utf-8 -*- from selenium import webdriver import unittest,time import HTMLTestRun ...
- Python+Selenium 自动化实现实例-模块化调用
public 目录存一些公共模块,供用例调用.login.py 内容如下: # coding=utf-8 import time # login def login(driver): driver.f ...
- Python+Selenium 自动化实现实例-数据驱动实例
#coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.implicitly_wait(10) ...
- Python+Selenium 自动化实现实例-获取测试对象的Css属性
#coding:utf-8 '''获取测试对象的css属性场景 当你的测试用例纠结细枝末节的时候,你就需要通过判断元素的css属性来验证你的操作是否达到了预期的效果.比如你可以通过判断页面上的标题字号 ...
随机推荐
- MACD判断定背离,底背离
MACD背离: 价格创新高而指标却下跌 价格创新低而指标却上涨 缠中说禅背离 多次缠绕中唇吻的面积更小 看图说话:
- 让网络编程更轻松和有趣 t-io
原文:https://www.oschina.net/p/t-io 注意:还是尽量去看原文,因为原文下面的评论也很有意思,可以参考大牛的讨论学习到新的东西 授权协议:Apache 开发语言:Java ...
- idea 配置maven
1.情景展示 idea如何配置maven 2.解决方案 设置 输入maven-->更改maven的根目录 更改默认的setting.xml的位置 改成maven下的默认settings.xm ...
- rabbitmq重装依赖的erlang 要注意
今天安装的erlang和rabbitmq版本不匹配导致出现各种问题,在使用正确版本安装后出现问题,在日志中找到报错信息: {"init terminating in do_boot" ...
- [Jmeter系列]Jmeter源码编译步骤(转)
官网:http://jmeter.apache.org/building.html 1,在apach官网download源码: http://jmeter.apache.org/download_ ...
- Java Web(5) Spring 下使用Junit4 单元测试
1. 如何在不启动Tomcat服务器的情况下对,Dao这些不依赖使用Servlet API的类来进行单元测试呢? 其实在Spring框架体系中,已经有一套自己的测试代码,其中就是依赖使用Junit来进 ...
- 利用Git进行团队协作
前言: 这里简单介绍一下Git的历史. 同生活中的许多伟大事件一样,Git 诞生于一个极富纷争大举创新的年代.Linux 内核开源项目有着为数众广的参与者.绝大多数的 Linux 内核维护工作都花在了 ...
- 怎样查看SSL证书的有效期?自动续期是否生效?
前面一篇教程教大家如何能够把网站的 HTTPS 的 SSL 证书自动续期.料神米课的学员动手能力都很强,已经很多都成功把证书续期了.但怎么看证书续期是否成功了呢? 使用火狐 firefox 浏览器就可 ...
- 开发 Swift 和 Objective-C 混编的 Framework
来源:黄文臣 blog.csdn.net/hello_hwc/article/details/58320433 前言 为什么要写这样一篇文章,因为昨天和一个朋友讨论到Swift和Objective C ...
- 【DeepLearning】Exercise:Sparse Autoencoder
Exercise:Sparse Autoencoder 习题的链接:Exercise:Sparse Autoencoder 注意点: 1.训练样本像素值需要归一化. 因为输出层的激活函数是logist ...