Selenium WebDriver使用EC来定义显式等待的条件

Selenium WebDriver中用于等待页面元素加载的方法,有不同的工作方式:

1. `implicitly_wait(time_to_wait)`:

  • 这是一种隐式等待方法,它在查找元素时会等待一定的时间。

  • 它会在每次查找元素时等待一段固定的时间,如果元素在规定时间内加载完毕,就会立即执行后续的操作。

  • 如果元素没有在规定时间内加载完毕,WebDriver会抛出异常。

  • 这种等待方式会应用于整个 WebDriver 的生命周期中,只需要设置一次即可。

  • driver.implicitly_wait(10) # 设置隐式等待时间为10秒

2. `WebDriverWait`和`expected_conditions`是Selenium WebDriver中用于实现显式等待的两个重要组件。

. WebDriverWait:

  • `WebDriverWait`是一个等待类,它提供了等待页面元素加载的功能。

  • 通过创建一个`WebDriverWait`对象,你可以指定一个最大等待时间,并指定一个WebDriver实例和超时时间。

  • 它提供了`until()`方法,可以指定一个条件,当条件为真或超时时,等待结束。

. expected_conditions:

  • `expected_conditions`是一组预定义的条件,用于在等待期间检查元素是否满足某些条件。

  • 这些条件包括元素是否可见、元素是否存在、元素是否被点击等等。

  • 它们通常与`WebDriverWait`一起使用,在等待期间反复检查条件是否满足。

  • 这些条件存储在`expected_conditions`模块中,可以通过`from selenium.webdriver.support import expected_conditions`导入。

例如,等待元素可点击:

python 复制代码
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, 'elementID'))
)

隐式等待 会在每次查找元素时都等待一段时间,而显式等待 会在等待期间反复检查某个条件是否满足,只有满足条件或者超时才会继续执行。通常情况下,显式等待更加灵活且精确。

元素可见、元素存在以及元素包含特定文本, 示例:

  1. 元素可见(element_to_be_clickable):

    python 复制代码
    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.element_to_be_clickable((By.ID, 'elementID'))
    )
  2. 元素存在(presence_of_element_located):

    python 复制代码
    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, 'elementID'))
    )
  3. 元素包含特定文本(text_to_be_present_in_element):

    python 复制代码
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    element = WebDriverWait(driver, 10).until(
        EC.text_to_be_present_in_element((By.ID, 'elementID'), 'expected text')
    )

通过使用 WebDriverWaitexpected_conditions,可以设置等待时间并指定条件,直到条件满足或超时为止。这样可以确保在执行操作之前等待页面元素的出现或特定状态,提高测试脚本的稳定性和可靠性。


`expected_conditions`模块提供了一系列预定义的条件,用于等待页面元素的状态变化。这些条件可以与`WebDriverWait`一起使用,以实现显式等待,直到条件满足或者超时。

以下是一些常用的预定义条件:

  1. presence_of_element_located(locator):检查页面上是否存在指定的元素,存在则返回该元素对象。

  2. visibility_of_element_located(locator):检查页面上指定的元素是否可见,可见则返回该元素对象。

  3. element_to_be_clickable(locator):检查页面上指定的元素是否可见且可点击,可点击则返回该元素对象。

  4. text_to_be_present_in_element(locator, text_):检查指定元素中是否包含了指定的文本。

  5. title_is(title):检查当前页面的标题是否等于给定的标题。

  6. title_contains(partial_title):检查当前页面的标题是否包含给定的部分标题。

  7. alert_is_present():检查页面上是否存在警告对话框。

  8. presence_of_all_elements_located(locator):检查页面上是否存在至少一个元素,符合指定的定位器。

  9. invisibility_of_element_located(locator):检查页面上指定的元素是否不可见。

  10. element_to_be_selected(element):检查指定的下拉框元素是否被选中。

  11. element_located_to_be_selected(locator):检查指定的下拉框元素是否存在,并且被选中。

  12. element_selection_state_to_be(element, is_selected):检查指定的元素的选中状态是否符合预期。

  13. element_located_selection_state_to_be(locator, is_selected):检查指定的元素的选中状态是否符合预期。

这些条件可以根据需要进行组合使用,以实现更精确的等待,确保页面元素在特定状态下才进行后续操作。

相关推荐
爱学测试的雨果2 小时前
Postman —— postman实现参数化
软件测试·功能测试·测试工具·lua·postman
互联网杂货铺3 小时前
如何用Postman实现自动化测试?
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
冷月半明4 小时前
Python项目打包指南:PyInstaller与SeleniumWire的兼容性挑战及解决方案
python·selenium
niuniu_6666 小时前
安全性测试(Security Testing)
测试工具·单元测试·appium·测试·安全性测试
m0_490240677 小时前
软件自动化测试(1):python+selenium自动化测试环境搭建
开发语言·python·selenium
薄荷你玩_11 小时前
简单粗暴,用浏览器调试端口绕过Selenium/Playwright/Puppeteer检测
selenium·测试工具
程序员三藏11 小时前
Python+Jenkins+Allure Report接口自动化测试持续集成
自动化测试·软件测试·python·测试工具·ci/cd·jenkins·测试用例
星星点灯50812 小时前
盛铂科技FlexDDS-NG:12通道相位连续DDS信号发生器,400MHz高频输出赋能量子光学与超冷原子研究
驱动开发·科技·测试工具·量子计算·模块测试·射频工程
niuniu_66614 小时前
selenium应用测试场景
python·selenium·测试工具·单元测试·测试
天才测试猿1 天前
Selenium常用函数总结
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例