14、技巧之七: expected_conditions的用法详解【Selenium+Python3网页自动化总结】

2023-12-13 05:51:01

本文从含义,例子,和疑点解析三个方面介绍expected_conditions的用法,避免在编程过程中踩坑不断。

1、概念介绍

使用WebDriverWait和expected_conditions两个类来实现等待在网页自动化过程中的某个事件的发生,再执行下一个操作。这种用法称为显示等待。

2、事件详解

? presence_of_element_located

? title_is

? title_contains

? visibility_of_element_located

? visibility_of

? presence_of_all_elements_located

? text_to_be_present_in_element

? text_to_be_present_in_element_value

? frame_to_be_available_and_switch_to_it

? invisibility_of_element_located

? element_to_be_clickable

? staleness_of

? element_to_be_selected

? element_located_to_be_selected

? element_selection_state_to_be

? element_located_selection_state_to_be

? alert_is_present

Selenium 的 expected_conditions 模块提供了一组条件,用于等待直到满足特定条件后再执行下一步操作。以下是一些常见的 expected_conditions 用法、含义和例子:

?? presence_of_element_located(元素出现):

??????? 含义:等待直到页面上至少存在一个具有指定定位器的元素。

??????? 例子:
???

from selenium.webdriver.common.by import By

??? from selenium.webdriver.support.ui import WebDriverWait

??? from selenium.webdriver.support import expected_conditions as EC



??? element = WebDriverWait(driver, 10).until(

??????? EC.presence_of_element_located((By.ID, "element_id"))

??? )

title_is(title)

??? 含义:等待直到页面的标题等于给定的标题。

??? 例子:
???

title_condition = EC.title_is("Expected Title")

WebDriverWait(driver, 10).until(title_condition)

title_contains(title)(页面标题包含指定文本)

??? 含义:等待直到页面的标题包含给定的部分文本。

??? 例子:

??? title_contains_condition = EC.title_contains("Expected")

??? WebDriverWait(driver, 10).until(title_contains_condition)

visibility_of_element_located(locator)(元素可见)

??? 含义:等待直到页面上至少存在一个具有指定定位器的元素,并且该元素可见。

??? 例子:

??? visibility_condition = EC.visibility_of_element_located((By.ID, "element_id"))

??? WebDriverWait(driver, 10).until(visibility_condition)

visibility_of(element)(元素可见)

??? 含义:等待直到给定的元素可见。

??? 例子:

? element = driver.find_element(By.ID, "element_id")

??visibility_condition = EC.visibility_of(element)

??WebDriverWait(driver, 10).until(visibility_condition)

presence_of_all_elements_located(locator) (具有指定定位器的元素都出现)

??? 含义:等待直到页面上所有具有指定定位器的元素都出现。

??? 例子:

??? all_elements_condition = EC.presence_of_all_elements_located((By.CLASS_NAME, "elements_class"))

??? WebDriverWait(driver, 10).until(all_elements_condition)

text_to_be_present_in_element(locator, text)(元素文本包含指定文本)

??? 含义:等待直到指定元素的文本包含给定的文本。

??? 例子:

??? text_condition = EC.text_to_be_present_in_element((By.ID, "element_id"), "Expected Text")

??? WebDriverWait(driver, 10).until(text_condition)

text_to_be_present_in_element_value(locator, text)

??? 含义:等待直到指定元素的值(通常是输入字段的值)包含给定的文本。

??? 例子:

??? text_value_condition = EC.text_to_be_present_in_element_value((By.ID, "element_id"), "Expected Value")

??? WebDriverWait(driver, 10).until(text_value_condition)

frame_to_be_available_and_switch_to_it(locator)

??? 含义:等待直到具有指定定位器的 iframe 出现,并切换到该 iframe。

??? 例子:
???

 frame_condition = EC.frame_to_be_available_and_switch_to_it((By.ID, "frame_id"))

?WebDriverWait(driver, 10).until(frame_condition)

invisibility_of_element_located(locator)(元素不可见)

??? 含义:等待直到页面上不存在具有指定定位器的元素或该元素不可见。

??? 例子:

??? invisibility_condition = EC.invisibility_of_element_located((By.ID, "element_id"))

??? WebDriverWait(driver, 10).until(invisibility_condition)

??

element_to_be_clickable(locator)(元素可点击)

??? 含义:等待直到页面上至少存在一个具有指定定位器的元素,并且该元素是可点击的。

??? 例子:

??? clickable_condition = EC.element_to_be_clickable((By.ID, "element_id"))

??? WebDriverWait(driver, 10).until(clickable_condition)

staleness_of(element)

??? 含义:等待直到给定元素不再附加到 DOM。

??? 例子:


??? staleness_condition = EC.staleness_of(element)

??? WebDriverWait(driver, 10).until(staleness_condition)

element_to_be_selected(element)

??? 含义:等待直到给定的元素被选中。

??? 例子:

?? selected_condition = EC.element_to_be_selected(element)

???WebDriverWait(driver, 10).until(selected_condition)

element_located_to_be_selected(locator)

??? 含义:等待直到具有指定定位器的元素被选中。

??? 例子:

? located_selected_condition = EC.element_located_to_be_selected((By.ID, "element_id"))

??WebDriverWait(driver, 10).until(located_selected_condition)

element_selection_state_to_be(element, is_selected)

??? 含义:等待直到给定元素的选中状态符合预期。

??? 例子:

??? selection_state_condition = EC.element_selection_state_to_be(element, True)

??? WebDriverWait(driver, 10).until(selection_state_condition)

element_located_selection_state_to_be(locator, is_selected)

??? 含义:等待直到具有指定定位器的元素的选中状态符合预期。

??? 例子:

??? located_selection_state_condition = EC.element_located_selection_state_to_be((By.ID, "element_id"), True)

??? WebDriverWait(driver, 10).until(located_selection_state_condition)

alert_is_present()

??? 含义:等待直到存在警告框(Alert)。

??? 例子:

alert_condition = EC.alert_is_present()

WebDriverWait(driver, 10).until(alert_condition)

3、疑点解惑

1、为什么expected_conditions这些方法的参数有两个括号?

比如presence_of_element_located((by, locator))因为参数(by, locator)表示一个元组,? 参数名称叫做locator。程序运行时,先把入参赋值:locator=(by, locator),? 然后在使用参数前加一个"*"号把元组解包出来变成by, locator两个参数传给driver.find_element, 就变成了driver.find_element(by,locator),比如这样:driver.find_element(By.ID, "xxx")的形式了。

#在源码是这样使用的:

def presence_of_element_located(locator: Tuple[str, str]) -> Callable[[AnyDriver], WebElement]:

??? """An expectation for checking that an element is present on the DOM of a

??? page. This does not necessarily mean that the element is visible.



??? locator - used to find the element

??? returns the WebElement once it is located

??? """



??? def _predicate(driver):

??????? return driver.find_element(*locator)



??? return _predicate

2、staleness_of什么时候用?

页面元素过期了,无法引用元素。意思就是之前一个操作会刷新网页,刷新了元素所在的页面。比如点了下一页的按钮后,可以用这个函数判断老的元素是不是过期了,相当于用这种排除法来确定新网页有没有加载。

示例代码:???

 def browser_login_user(self, username, password, facility_name=None, browser=None):

??????? """

??????? Tests that an existing admin user can log in.

??????? """

??????? browser = browser or self.browser

??????? self.browse_to(login_url, browser=browser)? # Load page



??????? username_field = browser.find_element_by_id("id_username")

??????? username_field.clear()? # clear any data

??????? username_field.click()? # explicitly set the focus, to start

??????? self.browser_form_fill(username, browser=browser)

??????? self.browser_form_fill(password, browser=browser)

??????? username_field.submit()



??????? # wait for 5 seconds for the page to refresh

??????? WebDriverWait(browser, 5).until(EC.staleness_of(username_field))

文章来源:https://blog.csdn.net/zxwscau/article/details/134922292
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。