Selenium系列教程 - WebDriverWait 详解以及自定义判断条件

1. WebDriverWait 参数与调用方法

from selenium.webdriver.support.wait import WebDriverWait
---------------------------------------------------------
WebDriverWait(driver, timeout, poll_frequency, ignored_exceptions)

下面来看参数:
driver:              传入WebDriver实例
timeout:             超时时间,等待的最长时间(同时要考虑隐性等待时间)
poll_frequency:      调用until或 中的方法的间隔时间,默认是 0.5 秒
ignored_exceptions:  忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException
---------------------------------------------------------
两种等待方式:until(method, message)、 until_not(method, message)
method:    在等待期间,每隔一段时间调用这个传入的方法,until是当某元素出现或什么条件成立则继续执行,until_not是当某元素消失或什么条件不成立则继续执行
message:   如果超时,抛出TimeoutException,将message传入异常

2. expected_conditions 模块

上面的 method 参数可以用 selenium 提供的 expected_conditions 模块中的各种条件,也可以用 WebElement 的 is_displayed() 、is_enabled()、is_selected() 方法,或者用自己封装的方法都可以,但是不能传 WebElement 对象。接下来我们看一下 expected_conditions 提供的条件有哪些:

from selenium.webdriver.support import expected_conditions
---------------------------------------------------------
# 以下两个条件类验证title,验证传入的参数title是否等于或包含于driver.title
title_is
title_contains

# 以下四个条件类验证url,验证driver.current_url
url_contains	# 传入url,包含时返回true
url_matches		# 传入正则表达式
url_to_be		# 传url,完全匹配时返回true
url_changes		# 传url,不匹配时返回true

# 以下两个条件验证元素是否出现,传入的参数都是元组类型的locator
presence_of_element_located
presence_of_all_elements_located

# 以下条件验证元素是否可见,visibility验证可见,invisibility验证不可见
visibility_of							# 传入WebElement
visibility_of_element_located			# 传入元组类型的locator
visibility_of_any_elements_located		# 传入元组类型的locator
visibility_of_all_elements_located		# 传入元组类型的locator
invisibility_of_element					# 传入WebElement 或 locator
invisibility_of_element_located			# 传入元组类型的locator

# 以下两个条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的value 
text_to_be_present_in_element
text_to_be_present_in_element_value

# 以下条件判断frame是否可切入,可切入则切入指定frame
frame_to_be_available_and_switch_to_it

# 以下条件判断元素是可见的并且可被点击
element_to_be_clickable

# 以下条件等某个结点从DOM树中移除,如果该元素仍然存在则返回false,否则返回true,常用于判断页面是否已刷新
staleness_of

# 以下四个条件判断元素是否被选中
element_to_be_selected					# 传入WebElement对象
element_located_to_be_selected			# 传入元组类型的locator
element_selection_state_to_be			# 传入WebElement对象以及状态,相等返回True,否则返回False
element_located_selection_state_to_be	# 传入locator以及状态,相等返回True,否则返回False 

# 以下条件判断窗口数量,传入期望窗口数
number_of_windows_to_be

# 以下条件判断是否打开一个新窗口,并且窗口句柄数量会增加
new_window_is_opened

# 以下条件判断是否有alert出现 
alert_is_present

3. WebDriverWait 与 expected_conditions 示例代码

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.maximize_window()
driver.implicitly_wait(10)

driver.get("http://www.baidu.com/")

locator = ("id", "kw")
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))
element.send_keys("hwijew")

time.sleep(2)
driver.quit()

4. 自定义条件

4.1 使用 lambda

WebDriverWait(driver, 5).until(lambda x: x.find_element(by="id", value="kw"))

其中 lambda 后的 x 是默认传过来的 driver,由 WebDriverWait(driver, 10) 中的 driver 确定。

4.2 封装一个类

封装的类必须要有__call__方法,通过这个方法返回判断的结果,call 方法的参数,是 until 中默认传过来的driver,__init__中的参数,是类初始化时传过来的参数,如要传多个参数就从这里下手

# 自定义判断方法
class WaitElement(object):

    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        from selenium.common.exceptions import NoSuchElementException
        try:
            return driver.find_element(self.locator)
        except NoSuchElementException as e:
            raise e
---------------------------------------------------------
# 调用
WebDriverWait(driver, 10).until(WaitElement(locator))

在这里插入图片描述


在这里插入图片描述

  • 17
    点赞
  • 98
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值