web ui自动化之显式等待的等待条件 - expected_conditions

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

本文将对selenium中的WebDriverWait模块和expected_conditions模块进行介绍。

WebDriverWait模块是selenium为WebDriver提供的显式等待方法,该模块需要传入等待条件方能生效。

expected_conditions模块是Selenium 提供的等待条件列表,它可以为WebDriverWait提供等待条件,一般将该模块导入为 EC,并将该模块提供的方法传递给 WebDriverWait.until() 或 WebDriverWait.until_not()

导入模块:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


WebDriverWait模块介绍

WebDriverWait参数介绍:

driver:WebDriver 实例
timeout:超时时间,单位为秒
poll_frequency:
轮询间隔(即每隔多少秒查询一次条件是否满足),默认为 0.5 秒
ignored_exceptions:需要忽略的异常列表

WebDriverWait语法示例:

WebDriverWait(self.driver,10,1)

WebDriverWait模块中提供了两个方法:

def until(self, method, message=‘’):
“”“每次轮询时调用 method,直到 method 的返回不等价于 False。
即等待 method 描述的条件被满足。
“””
def until_not(self, method, message=‘’):
“”“每次轮询时调用 method,直到 method 的返回等价于 False。
即等待 method 描述的条件不被满足。
“””
参数:
method:每次轮询时要执行的方法
message:轮询超时后传递给超时异常的文本消息

expected_conditions模块介绍

根据title判断

title_is(title)

判断当前页面的title是否完全等于预期字符串,返回布尔值(可以通过F12–元素定位去查看title)

WebDriverWait(self.driver,10).until(EC.title_is('百度一下,你就知道'))

title_contains(title)

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

WebDriverWait(self.driver,10).until(EC.title_contains('百度一下'))

根据url判断

url_contains(url)

判断url是否包含特定内容,如果是返回True,否则返回False

WebDriverWait(self.driver,20).until(EC.url_contains('https://apps.apple.com/'))

url_matches(pattern)

通过正则匹配url,网页网址是否匹配特定内容,如果是返回True,否则返回False。pattern是匹配url的正则表达式样式

pattern = r'.*?baidu.*?' #使用正则表达式,规定网址格式(格式要求以https开头)
WebDriverWait(self.driver,10).until(EC.url_matches(pattern))

url_to_be(url)

判断网页网址是否显示特定网址(必须完全符合),如果是返回True,否则返回False

WebDriverWait(self.driver,10).until(EC.url_to_be('https://www.baidu.com/'))

url_changes(url)

检查url是否改变成传入的url了,如果url不同,则返回True,否则返回false。

WebDriverWait(self.driver,10).until(EC.url_contains('https://www.baidu.com/'))

根据元素是否存在或可见判断

presence_of_element_located(locator)

判断某个元素是否被加到DOM(document object model 文档对象模型)树下,不代表该元素一定可见。如果是,返回该元素(单个元素),否则报错

WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.LINK_TEXT,'新闻')))

presence_of_all_elements_located(locator)

定位的元素范围内,是否至少有一个元素存在于页面当中,如果是,返回满足条件的所有元素组成的List,否则返回空List.

WebDriverWait(self.driver,10).until(EC.presence_of_all_elements_located((By.LINK_TEXT,'新闻')))

visibility_of_element_located(locator)

判断某个元素是否可见,可见代表元素非隐藏,并且元素的宽和高都不为0,如果是返回该元素(单个元素),否则报错。
与 presence_of_element_located作用类似,但前者无所谓可见与否,所以在性能上略胜一筹

WebDriverWait(self.driver,10).until(EC.visibility_of_element_located((By.LINK_TEXT,'新闻')))

invisibility_of_element_located(locator)

判断某个元素是否不存在于dom树或不可见,如果不存在则返回True,否则返回False。与 visibility_of_element_located的作用相反

WebDriverWait(self.driver,10).until(EC.invisibility_of_element_located((By.LINK_TEXT,'新闻11122')))

invisibility_of_element(element)

特定元素是否不可访问或不存在于DOM树中,如果不存在则返回True,否则返回False。与invisibility_of_element_located作用类似,但它传的是定位范围locator,而这个可以是定位范围(仅text文本),也可以是元素element。

element = By.LINK_TEXT,'新闻11122'
        WebDriverWait(self.driver,10).until(EC.invisibility_of_element(element))

visibility_of_any_elements_located(locator)

检查locator 指定的元素中至少一个可见,定位器-用于查找元素,找到并可见WebElement后返回WebElement的列表

WebDriverWait(self.driver,10).until(EC.visibility_of_any_elements_located((By.LINK_TEXT,'新闻')))

visibility_of_all_elements_located(locator)

检查所有元素是否都存在于页面显示和可见。可见性意味着不仅显示元素,并且页面可以查看到。定位器-用于查找元素,找到并可见WebElement后返回WebElement的列表

WebDriverWait(self.driver,10).until(EC.visibility_of_all_elements_located((By.LINK_TEXT,'新闻')))

根据text文本判断

text_to_be_present_in_element(locator, text_)

判断locator 指定的元素上包含文本 text_

WebDriverWait(self.driver,10).until(EC.text_to_be_present_in_element((By.LINK_TEXT,'新闻'),'新'))

text_to_be_present_in_element_value(locator, text_)

判断locator 指定的元素的 value 属性值包含文本 text_

在这里插入代码片

text_to_be_present_in_element_attribute(locator, attribute_, text_)

判断locator 指定的元素的 attribute_ 指定的属性值中包括文本 text_

在这里插入代码片

根据frame与alert判断

frame_to_be_available_and_switch_to_it(locator)

判断该frame是否可以switch进去,如果可以,则返回True并且switch进去,否则返回False

alert_is_present()

判断页面上是会否存在alert,如果存在,切换到alert,否则返回false。判断是否存在后,可对alert进行操作,如关闭alert

WebDriverWait(self.driver,10).until(EC.alert_is_present())

根据windows窗口判断

number_of_windows_to_be(num_windows)

特定窗口数和实际窗口数是否一致,如果是返回True,否则返回False

WebDriverWait(self.driver, 10).until(EC.number_of_windows_to_be(1))

new_window_is_opened(current_handles)

current_handles = self.driver.window_handles #获得当前所有句柄数量
new_window = EC.new_window_is_opened(current_handles)
print(new_window(self.driver))
WebDriverWait(driver,10).until(EC.new_window_is_opened(current_handles) )

根据元素是否被选中/移除判断

element_to_be_clickable(mark)

判断元素可见并可点击,mark 即可以是 locator,也可以是 WebElement

staleness_of(element)

判断元素 element 不再属于 DOM(等某个元素从DOM树下移除)

element_to_be_selected(element)

判断某个元素是否被选中,一般用于select下拉表

element_located_to_be_selected(locator)

判断locator 指定的元素是否被选中

element_selection_state_to_be(element, is_selected)

元素 element 的选中状态符合 is_selected,True 表示被选中,False 表示未选中。

element_located_selection_state_to_be(locator, is_selected)

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

element_attribute_to_include(locator, attribute_)

判断locator 指定的元素包含 attribute_ 指定的属性

根据复合条件判断

复合条件即将上面列出的等待条件通过逻辑操作符连接起来,逻辑操作包括:与、或、非。

any_of(*expected_conditions)

expected_conditions 列表中任何一个条件被满足

all_of(*expected_conditions)

expected_conditions 列表中条件都被满足

none_of(*expected_conditions)

expected_conditions 列表中任何一个条件都不符合

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值