Selenium—显式等待中的EC模块参数大全

        最近学到了expected_conditions模块(在使用时通常重命名为EC模块),在显式等待WebDriverWait的until和until_not方法中我们经常要用到,它会根据网页标题、网址以及元素是否可见等条件来决定我们是否需要继续等待。后面又使用到了其他参数,根据各大类型对它们做了个整理,方便记忆。

        因为要使用 expected_conditions(EC)模块中的类,所以第一步肯定是要引入该模块,考虑到引用类时,很多地方都用到了locator定位范围,所以这里我们还要引入必须要引入  selenium.webdriver.common.by  的 By 模块,再加上一个超时等待,具体代码如下:

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

#为方便点击后事件能够完整触发,导入sleep
from time import sleep

        话不多说,先放示例:以下为CSS选择器根据"download_button"查找并点击(根据需要继续调整即可),超时时间设为6s

WebDriverWait(driver, 6).until(EC.element_to_be_clickable((By.CSS_SELECTOR, download_button))).click()

sleep(.5)#根据需要自己调整等待时长,此处仅做参考

1. element_to_be_clickable(locator)

  • 说明: 检查元素是否既可见又可点击。
  • 用法: WebDriverWait(driver, timeout).until(EC.element_to_be_clickable(locator))
  • 返回: 可点击的Web元素。

2. element_to_be_selected(element)

  • 说明: 检查元素是否被选中(适用于或复选框等元素)。
  • 用法: WebDriverWait(driver, timeout).until(EC.element_to_be_selected(element))
  • 返回: True 如果元素被选中。

3. element_selection_state_to_be(element, selected)

  • 说明: 检查元素的选中状态是否符合期望。
  • 用法: WebDriverWait(driver, timeout).until(EC.element_selection_state_to_be(element, selected))
  • 返回: True 如果元素的选中状态符合预期。

4. element_located_selection_state_to_be(locator, selected)

  • 说明: 检查通过locator查找的元素是否已被选中。
  • 用法: WebDriverWait(driver, timeout).until(EC.element_located_selection_state_to_be(locator, selected))
  • 返回: True 如果元素被选中。

5. visibility_of(element)

  • 说明: 检查元素是否可见(即存在且没有display: nonevisibility: hidden)。
  • 用法: WebDriverWait(driver, timeout).until(EC.visibility_of(element))
  • 返回: 可见的Web元素。

6. visibility_of_all_elements_located(locator)

  • 说明: 检查通过locator定位的所有元素是否都可见。
  • 用法: WebDriverWait(driver, timeout).until(EC.visibility_of_all_elements_located(locator))
  • 返回: 可见的元素列表。

7. presence_of_element_located(locator)

  • 说明: 检查元素是否在DOM中存在。
  • 用法: WebDriverWait(driver, timeout).until(EC.presence_of_element_located(locator))
  • 返回: 元素本身,不一定可见。

8. presence_of_all_elements_located(locator)

  • 说明: 检查通过locator定位的所有元素是否在DOM中存在。
  • 用法: WebDriverWait(driver, timeout).until(EC.presence_of_all_elements_located(locator))
  • 返回: 存在的元素列表。

9. staleness_of(element)

  • 说明: 检查元素是否已经不再附加到DOM中,通常用于检查页面是否刷新。
  • 用法: WebDriverWait(driver, timeout).until(EC.staleness_of(element))
  • 返回: True 如果元素已经变得过时。

10. alert_is_present()

  • 说明: 检查是否有警告框存在。
  • 用法: WebDriverWait(driver, timeout).until(EC.alert_is_present())
  • 返回: 如果警告框存在,则返回Alert对象。

11. text_to_be_present_in_element(locator, text_)

  • 说明: 检查元素的文本内容是否包含指定的文本。
  • 用法: WebDriverWait(driver, timeout).until(EC.text_to_be_present_in_element(locator, text_))
  • 返回: True 如果文本匹配。

12. text_to_be_present_in_element_value(locator, text_)

  • 说明: 检查元素的值是否包含指定文本(适用于表单元素)。
  • 用法: WebDriverWait(driver, timeout).until(EC.text_to_be_present_in_element_value(locator, text_))
  • 返回: True 如果文本匹配。

13. frame_to_be_available_and_switch_to_it(locator)

  • 说明: 检查某个iframe是否可用,并切换到该iframe。
  • 用法: WebDriverWait(driver, timeout).until(EC.frame_to_be_available_and_switch_to_it(locator))
  • 返回: 如果iframe可用,则返回True

14. invisibility_of_element_located(locator)

  • 说明: 检查元素是否不可见(可能不在DOM中或display: none)。
  • 用法: WebDriverWait(driver, timeout).until(EC.invisibility_of_element_located(locator))
  • 返回: True 如果元素不可见。

15. invisibility_of_element(element)

  • 说明: 检查元素是否不可见。
  • 用法: WebDriverWait(driver, timeout).until(EC.invisibility_of_element(element))
  • 返回: True 如果元素不可见。

16. element_to_be_selected(locator)

  • 说明: 检查元素是否被选中。
  • 用法: WebDriverWait(driver, timeout).until(EC.element_to_be_selected(locator))
  • 返回: True 如果元素被选中。

17. element_is_enabled(element)

  • 说明: 检查元素是否可用(即没有disabled属性)。
  • 用法: WebDriverWait(driver, timeout).until(EC.element_is_enabled(element))
  • 返回: True 如果元素可用。

这些条件常用于配合WebDriverWait来控制等待,确保在执行操作之前,目标元素处于理想状态。

下面放些不常用但可能有用的选项:

18. element_to_be_selected(locator)

  • 说明: 检查通过定位器查找的元素是否被选中。
  • 用法: WebDriverWait(driver, timeout).until(EC.element_to_be_selected(locator))
  • 返回: True 如果元素被选中。

19. frame_to_be_available_and_switch_to_it(locator)

  • 说明: 检查指定的框架(frame)是否可用并切换到它。
  • 用法: WebDriverWait(driver, timeout).until(EC.frame_to_be_available_and_switch_to_it(locator))
  • 返回: 如果frame可用并切换成功,返回True

20. number_of_windows_to_be(number)

  • 说明: 检查当前浏览器窗口的数量是否等于指定的数量。
  • 用法: WebDriverWait(driver, timeout).until(EC.number_of_windows_to_be(number))
  • 返回: 如果浏览器窗口数符合要求,返回True

21. new_window_is_opened(current_handles)

  • 说明: 检查是否有新的浏览器窗口被打开,适用于多窗口切换。
  • 用法: WebDriverWait(driver, timeout).until(EC.new_window_is_opened(current_handles))
  • 返回: 如果新的窗口打开,返回True

22. title_is(title)

  • 说明: 检查当前页面的标题是否与指定的标题匹配。
  • 用法: WebDriverWait(driver, timeout).until(EC.title_is(title))
  • 返回: 如果页面标题匹配,返回True

23. title_contains(title)

  • 说明: 检查当前页面的标题是否包含指定的文本。
  • 用法: WebDriverWait(driver, timeout).until(EC.title_contains(title))
  • 返回: 如果页面标题包含指定文本,返回True

24. url_to_be(url)

  • 说明: 检查当前页面的URL是否与指定的URL匹配。
  • 用法: WebDriverWait(driver, timeout).until(EC.url_to_be(url))
  • 返回: 如果URL匹配,返回True

25. url_contains(partial_url)

  • 说明: 检查当前页面的URL是否包含指定的部分字符串。
  • 用法: WebDriverWait(driver, timeout).until(EC.url_contains(partial_url))
  • 返回: 如果URL包含指定部分,返回True

26. url_changes(url)

  • 说明: 检查当前页面的URL是否发生了变化。
  • 用法: WebDriverWait(driver, timeout).until(EC.url_changes(url))
  • 返回: 如果URL发生变化,返回True

27. javaScriptThrowsException(script)

  • 说明: 检查执行JavaScript代码是否抛出异常。
  • 用法: WebDriverWait(driver, timeout).until(EC.javaScriptThrowsException(script))
  • 返回: 如果JavaScript执行抛出异常,返回True

28. element_located_to_be_selected(locator)

  • 说明: 检查通过定位器查找到的元素是否被选中。
  • 用法: WebDriverWait(driver, timeout).until(EC.element_located_to_be_selected(locator))
  • 返回: True 如果元素被选中。

你可能感兴趣的:(selenium,测试工具)