自动化测试 / 系统课时

验证元素状态

验证元素状态

元素“存在于 DOM”并不代表它现在可见、可交互或已被选中。Selenium 提供 is_displayed()、is_enabled() 和 is_selected() 三个布尔接口,本课用同一页面验证三种状态,并说明它们各自适合检查什么。

学习目标

  • 用 is_displayed() 判断元素当前是否对用户可见
  • 用 is_enabled() 检查控件是否启用
  • 用 is_selected() 验证复选框、单选框或选项的选中状态

三个状态分别回答什么

is_displayed() 返回元素当前是否显示给用户;is_enabled() 返回元素是否启用;is_selected() 主要用于 checkbox、radio 和 option 等可选择控件。三者都返回布尔值,但回答的问题不同,不能互相替代。

方法 True 代表 常见对象
is_displayed() 当前可见 文本、按钮、面板
is_enabled() 当前启用 input、button、select
is_selected() 当前已选中 checkbox、radio、option

例如一个按钮可以“可见但禁用”,所以 is_displayed() 为 True 并不能证明它能接受正常交互。复选框是否勾选则应直接读取 is_selected(),不要只检查 checked 字符串是否存在。

用固定页面验证三种状态

下面示例创建一个隐藏提示、一个禁用按钮和一个已勾选复选框。脚本一次读取全部状态,适合直接作为状态断言的最小模板。

python
from urllib.parse import quote

from selenium import webdriver
from selenium.webdriver.common.by import By

html = """
<!doctype html>
<html>
  <body>
    <div id="notice" style="display: none">Saved</div>
    <button id="submit" disabled>Submit</button>
    <input id="agree" type="checkbox" checked>
  </body>
</html>
"""

# 页面固定包含隐藏、禁用和已选中三类元素。
driver = webdriver.Chrome()

try:
    driver.get("data:text/html;charset=utf-8," + quote(html))

    notice = driver.find_element(By.ID, "notice")
    submit = driver.find_element(By.ID, "submit")
    agree = driver.find_element(By.ID, "agree")

    # 分别查询显示、启用和选中状态。
    print(f"notice displayed={notice.is_displayed()}")
    print(f"submit displayed={submit.is_displayed()}")
    print(f"submit enabled={submit.is_enabled()}")
    print(f"agree selected={agree.is_selected()}")
finally:
    driver.quit()

预期输出

text
notice displayed=False
submit displayed=True
submit enabled=False
agree selected=True

结果里按钮同时满足“显示”和“禁用”,说明状态检查应按业务条件组合,而不是只看一个布尔值。对原生表单控件,disabled 会影响启用状态;如果自定义组件只用 class 或 aria-disabled="true" 模拟禁用,则还要按组件约定检查对应属性。

把状态变成可读的断言

直接写布尔断言最清楚,失败时最好补上业务语义。片段示例:下面代码依赖前一段示例已经取得的 notice、submit 和 agree。

python
assert not notice.is_displayed(), "保存提示不应提前显示"
assert not submit.is_enabled(), "未满足条件时提交按钮应禁用"
assert agree.is_selected(), "默认协议复选框应保持选中"

预期结果

text
三个断言均通过,无控制台输出。

不要用 find_element() 成功来代替可见性断言:它只能说明元素已被定位到。也不要用 CSS class 名猜状态,例如 "disabled" in class 只能验证样式约定,不能等价替代 is_enabled() 对原生控件启用状态的判断。

动态页面先等待再验证

异步界面常见顺序是:元素先出现,随后才解除 disabled 或切换选择状态。此时立即读取状态容易产生偶发失败。对“等待可点击”可使用 WebDriverWait 配合 element_to_be_clickable;它要求目标可见且启用。对选中状态,可使用 element_to_be_selected 或 element_located_to_be_selected。

片段示例:以下代码依赖已创建的 driver,并需要页面中的 #submit 会在业务条件满足后变为可见且启用。

python
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.ID, "submit")))
assert button.is_displayed()
assert button.is_enabled()

预期结果

text
如果按钮在 10 秒内变为可见且启用,断言通过;否则等待抛出 TimeoutException。

这里的 10 是最长等待秒数,不代表固定休眠 10 秒。状态一旦满足,等待会提前结束;如果页面永远不会启用按钮,则应继续排查业务条件,而不是盲目增加超时时间。

按自己的节奏,确认真正理解。

目录与阅读设置

阅读设置

17

记录一个想法

验证元素状态

0 / 10000 · 笔记保存到账号。未提交的修改会在关闭、离开或刷新后丢失。

登录哪都通