主要内容
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))