Selenium系列教程 - 使用 expected_conditions 判断元素

本文详细介绍了Selenium库中的expected_conditions模块,包括各种条件类的使用,如验证页面标题、URL、元素可见性和可点击性等。这些条件通常与WebDriverWait结合使用,以实现更精确的页面元素等待和交互。示例代码展示了如何等待特定元素出现并输入文本,然后关闭浏览器。通过expected_conditions,开发者可以编写更健壮的自动化测试脚本。

1. expected_conditions 模块

我们看一下 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

# 以下三个条件验证元素是否可见,前两个传入参数是元组类型的locator,第三个传入WebElement 
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

2. 使用场景

expected_conditions 模块最常见的就是与 WebDriverWait 配合使用,如:

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()

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

在这里插入图片描述


在这里插入图片描述

### Selenium `expected_conditions` 模块基础知识 #### 导入模块并重命名 为了简化代码编写,在使用 `expected_conditions` 模块时通常会对其进行重命名,通过 `as` 关键字将其重命名为 `EC`[^1]。 ```python from selenium.webdriver.support import expected_conditions as EC ``` #### 使用场景说明 此模块主要用于对页面元素的状态进行判断,确保在执行操作前满足特定条件。这有助于提高脚本稳定性,防止因元素未加载完成而导致的操作失败[^3]。 #### 常见方法介绍及其应用实例 ##### 判断元素存在与否 可以利用 `presence_of_element_located(locator)` 方法来确认某个定位器所指向的元素是否存在于DOM树中: ```python element_present = EC.presence_of_element_located((By.ID, 'myElement')) WebDriverWait(driver, 10).until(element_present) ``` ##### 验证元素可见性 对于那些不仅需要存在而且要可视化的元素,则应采用 `visibility_of_element_located(locator)` 来验证其状态: ```python element_visible = EC.visibility_of_element_located((By.CLASS_NAME, "example")) WebDriverWait(driver, 10).until(element_visible) ``` ##### 确认元素可交互(即能被点击) 当目标是在找到之后立即对该元素实施点击动作时,应该先调用 `element_to_be_clickable(locator)` 进行检测: ```python clickable_element = EC.element_to_be_clickable((By.LINK_TEXT, 'Click Me!')) WebDriverWait(driver, 10).until(clickable_element).click() ``` ##### 元素过期/失效检查 有时还需要知道某元素何时不再属于当前文档结构内;这时就可以借助于 `staleness_of(element)` 完成这项工作 : ```python old_element = driver.find_element(By.CSS_SELECTOR, "#some-element") # 执行某些导致旧元素消失的动作... WebDriverWait(driver, 10).until(EC.staleness_of(old_element)) new_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, "#replacement-element"))) ``` 以上就是关于如何运用 `expected_conditions` 中几个典型函数的例子展示,这些工具可以帮助开发者更灵活有效地控制浏览器行为,从而实现更加稳定可靠的自动化测试流程[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值