Python+selenium自动化之27----EC模块之title_is

在自动化测试中,判定元素是否存在是最常用到的,在Python+selenium自动化之25----判定元素是否存在中介绍一种方法,下面的几篇介绍selenium中一个模块expected_conditions,简称EC。​

引用EC

如果需要使用EC,那么首先导入expected_conditions模块。

Python+selenium自动化之27----EC模块之title_is

EC模块16中判定方法

包含的判定方法很多,可以通过查看expected_conditions.py的源码。

Python+selenium自动化之27----EC模块之title_is

title_is:判断当前页面的title是否完全等于(==)预期字符串,返回是布尔值

title_contains 判断当前页面的title是否包含预期字符串,返回布尔值

presence_of_element_located:判断某个元素是否被加到了dom树里,并不代表该元素一定可见

visibility_of_element_located : 判断某个元素是否可见. 可见代表元素非隐藏,并且元素的宽和高都不等于0

visibility_of :跟上面的方法做一样的事情,只是上面的方法要传入locator,这个方法直接传定位到的element就好了

presence_of_all_elements_located : 判断是否至少有1个元素存在于dom树中。举个例子,如果页面上有n个元素的class都是'column-md-3',那么只要有1个元素存在,这个方法就返回True

text_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进去,否则返回False

invisibility_of_element_located : 判断某个元素中是否不存在于dom树或不可见

element_to_be_clickable : 判断某个元素中是否可见并且是enable的,这样的话才叫clickable

staleness_of :等某个元素从dom树中移除,注意,这个方法也是返回True或False

element_to_be_selected:判断某个元素是否被选中了,一般用在下拉列表>* element_selection_state_to_be:判断某个元素的选中状态是否符合预期

element_located_selection_state_to_be:跟上面的方法作用一样,只是上面的方法传入定位到的element,而这个方法传入locator

alert_is_present : 判断页面上是否存在alert

title_is:title判定方法

查看title_is方法的源码如下图所示:

Python+selenium自动化之27----EC模块之title_is

__init__是初始化内容,参数是title,必填项,__call__是把实例变成一个对象,参数是driver,返回的是self.title == driver.title,布尔值。

Python+selenium自动化之27----EC模块之title_is

 

Title_contains:部分匹配

和title_is方法类似,title_contains方法为部分匹配,当传入的字符串与当前窗口的title部分匹配时,则返回True。

 

Python+selenium自动化之27----EC模块之title_is

源码:

# -*- coding: UTF-8 -*-
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

driver
= webdriver.Firefox()
driver.implicitly_wait(
20)

# 打开腾讯企业邮箱
driver.get("https://exmail.qq.com/login")
#完全匹配判定
title1 = EC.title_is("腾讯企业邮箱-登录入口")
print (title1(driver))
#部分匹配判定
title2 = EC.title_contains("腾讯企业邮箱")
print(title2(driver))