【Python】Selenium WebDriver 中 `expected_conditions`(预期条件)模块

在 Selenium WebDriver 中,expected_conditions(预期条件)是一个特别有用的模块,它提供了一组预定义的等待条件,这些条件经常用于显式等待(Explicit Waits)中。显式等待允许我们的自动化脚本等待某个条件成立,或者等待某个元素出现在页面上,直到达到超时时间。

以下是一些常用的 expected_conditions:

  1. title_is - 等待页面标题与给定的字符串完全匹配。

    python 复制代码
    from selenium.webdriver.support import expected_conditions as EC
    WebDriverWait(driver, 10).until(EC.title_is("预期的标题"))
  2. title_contains - 等待页面标题包含给定的字符串。

    python 复制代码
    WebDriverWait(driver, 10).until(EC.title_contains("预期的标题部分"))
  3. presence_of_element_located - 等待页面上存在某个元素。

    python 复制代码
    from selenium.webdriver.common.by import By
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "element-id"))
    )
  4. element_located_to_be_clickable - 等待某个元素变得可点击(即元素是可见的,没有被其他元素遮挡,并且没有被禁用)。

    python 复制代码
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "button-id"))
    )
  5. element_to_be_selected - 等待某个元素被选中。

    python 复制代码
    WebDriverWait(driver, 10).until(
        EC.element_to_be_selected(driver.find_element_by_id("select-element-id"))
    )
  6. visibility_of_element_located - 等待某个元素可见(即元素在页面上显示,并且其宽高都大于0)。

    python 复制代码
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "element-id"))
    )
  7. visibility_of - 等待某个元素对象可见。

    python 复制代码
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of(driver.find_element_by_id("element-id"))
    )
  8. text_to_be_present_in_element - 等待某个元素中的文本包含特定的字符串。

    python 复制代码
    WebDriverWait(driver, 10).until(
        EC.text_to_be_present_in_element((By.ID, "element-id"), "预期的文本")
    )
  9. text_to_be_present_in_element_value - 等待某个元素的值包含特定的字符串。

    python 复制代码
    WebDriverWait(driver, 10).until(
        EC.text_to_be_present_in_element_value((By.ID, "input-id"), "预期的值")
    )
  10. frame_to_be_available_and_switch_to_it - 等待一个 <iframe> 元素可用,并切换到它。

    python 复制代码
    WebDriverWait(driver, 10).until(
        EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe-id"))
    )
  11. invisibility_of_element_located - 等待某个元素不再可见。

    python 复制代码
    WebDriverWait(driver, 10).until(
        EC.invisibility_of_element_located((By.ID, "element-id"))
    )
  12. element_to_be_clickable - 等待某个元素变得可点击。

    python 复制代码
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, "button-id"))
    )

这些预期条件可以与 WebDriverWait 结合使用,创建强大且灵活的等待逻辑,使你的自动化脚本能够适应动态变化的网页内容。通过显式等待,可以避免很多由于页面加载或元素状态变化导致的测试失败。

相关推荐
TomEval2 小时前
【测AI】第26篇:线上模型监控与告警体系 — 测试不是终点
人工智能·python·功能测试·ai
prince_zxill3 小时前
PEP 751 pylock.toml 实测:pip 26.3 装 6 个包版本全钉死,但 [web] 选不中任何东西
python·pip·依赖管理·pep 751
Resean02233 小时前
Spring 源码分析(二)配置类的解析与扫描
java·后端·spring
小毛球.3 小时前
第 3 篇:IDEA 最新版下载与完整安装教程(Mac)
java·macos·intellij-idea·idea
Thanks_ks4 小时前
Python 的垃圾回收——引用计数撑起日常,分代回收收拾残局
python·内存管理·垃圾回收·循环引用·引用计数·gc 模块·weakref
CyrusYiVPP4 小时前
实测 uv 0.13.7:同一套包,pip 装 94 秒,它只用 19 秒
人工智能·python
Misnearch5 小时前
ScheduledExecutorService
java·线程池
yiangL_5 小时前
Python 学习笔记:递归、turtle绘图、字符串与函数
笔记·python·学习
ashiho5 小时前
【LeetCode hot 100】3. 无重复字符的最长子串 (三种解法)
java·数据结构·后端·算法·leetcode·滑动窗口
lula8886 小时前
PVC-U、PVC-C、PVC-O 差一个字母,差的是三种增强路线
python