简体   繁体   English

为什么 Selenium python package 中没有 expected_conditions all_of、none_of 和 any_of?

[英]Why are expected_conditions all_of, none_of, and any_of absent in the Selenium python package?

I am using Selenium with Python and came across the need to use WebDriverWait and selenium.webdriver.support.expected_conditions in it.我正在使用WebDriverWaitselenium.webdriver.support.expected_conditions并遇到需要在 it.. I have to wait for an element to be removed and for another to be displayed, so I was wishing to use all_of and none_of to make the conditions easier to understand:我必须等待一个元素被删除并显示另一个元素,所以我希望使用 all_of 和 none_of 来使条件更容易理解:

WebDriverWait(browser, 10).until(
    EC.all_of(
        EC.presence_of_element_located((By.CSS_SELECTOR, "span[title='VMC OIC-2 batch 2022'")),
        EC.none_of(
            EC.presence_of_element_located((By.CSS_SELECTOR, "div#startup")),
            EC.presence_of_element_located((By.CSS_SELECTOR, "div#initial-startup"))
        )
    )
)

Unforunately, I received this error:不幸的是,我收到了这个错误:

AttributeError: module 'selenium.webdriver.support.expected_conditions' has no attribute 'all_of'

On further inspection through raw printing I got to know that none of the any_of, all_of and none_of conditions were working.通过原始打印的进一步检查,我知道 any_of、all_of 和 none_of 条件都不起作用。

Another thing to note is that the mentioned conditions are present in Selenium with Java.另一件需要注意的是,上述条件存在于 Selenium 和 Java 中。 I could find an issue talking about this, and it seems like any_of, all_of and none_of were eventually added to expected_conditions as is evident in the Selenium repository's corresponding file: expected_conditions.py (the last three functions).我可以找到一个谈论这个问题的问题,并且似乎 any_of、all_of 和 none_of 最终被添加到了 expected_conditions,这在 Selenium 存储库的相应文件中很明显: expected_conditions.py (最后三个函数)。

This question is demanding an explanation, not an alternative, I am well aware I can write a function to do the same task or even copy the function listed in expected_conditions.py but I would really like to know why these functions are present in the repository and absent in the working package.这个问题需要一个解释,而不是替代方案,我很清楚我可以写一个 function 来做同样的任务,甚至复制在 expected_conditions.py 中列出的 function 但我真的很想知道为什么这些函数存在于存储库中并且在工作的 package 中不存在。

Thanks for reading!谢谢阅读!

pip install selenium==4.0.0.a7

install selenium 4 alpha version, there will be no new updates in selenium 3.141安装 selenium 4 alpha 版本,selenium 3.141 中不会有新的更新

https://github.com/SeleniumHQ/selenium/releases https://github.com/SeleniumHQ/selenium/releases

you can see last update for v3 was on Dec 2018. click see new tags to see latest v4 releases您可以看到 v3 的最后一次更新是在 2018 年 12 月。单击查看新标签以查看最新的 v4 版本

在此处输入图像描述

the documentation is updated as per selenium 4文档根据 selenium 4 更新

https://github.com/SeleniumHQ/seleniumhq.github.io/issues/627 https://github.com/SeleniumHQ/seleniumhq.github.io/issues/627

They have been introduced in this commit .它们已在本次提交中介绍。 While the version in PyPi repository is 3.141 that commit will likely be delivered in 4.0.0 version.虽然PyPi 存储库中的版本是 3.141,但提交可能会在 4.0.0 版本中交付。

expected_conditions.all_of(*expected_conditions) expected_conditions.all_of(*expected_conditions)

selenium.webdriver.support.expected_conditions.all_of(*expected_conditions) is the expectation that all of multiple expected conditions is true. selenium.webdriver.support.expected_conditions.all_of(*expected_conditions)是期望所有多个预期条件都为真。 Equivalent to a logical AND .等效于逻辑AND It is defined as:它被定义为:

def all_of(*expected_conditions):
    """ An expectation that all of multiple expected conditions is true.
    Equivalent to a logical 'AND'.
    Returns: When any ExpectedCondition is not met: False.
    When all ExpectedConditions are met: A List with each ExpectedCondition's return value. """
    def all_of_condition(driver):
    results = []
    for expected_condition in expected_conditions:
        try:
        result = expected_condition(driver)
        if not result:
            return False
        results.append(result)
        except WebDriverException:
        return False
    return results
    return all_of_condition
    

An example一个例子

WebDriverWait(browser, 10).until(EC.all_of(
    EC.presence_of_element_located((By.CSS_SELECTOR, "span[title='VMC OIC-2 batch 2022']")),
    EC.presence_of_element_located((By.CSS_SELECTOR, "div#startup")),
    EC.presence_of_element_located((By.CSS_SELECTOR, "div#initial-startup"))
))

expected_conditions.any_of(*expected_conditions)预期条件.any_of(*预期条件)

selenium.webdriver.support.expected_conditions.any_of(*expected_conditions) is the expectation that all of multiple expected conditions is true. selenium.webdriver.support.expected_conditions.any_of(*expected_conditions)是期望所有多个预期条件都为真。 Equivalent to a logical AND .等效于逻辑AND It is defined as:它被定义为:

def any_of(*expected_conditions):
    """ An expectation that any of multiple expected conditions is true.
    Equivalent to a logical 'OR'.
    Returns results of the first matching condition, or False if none do. """
    def any_of_condition(driver):
    for expected_condition in expected_conditions:
        try:
        result = expected_condition(driver)
        if result:
            return result
        except WebDriverException:
        pass
    return False
    return any_of_condition

An example一个例子

WebDriverWait(browser, 10).until(EC.any_of(
    EC.presence_of_element_located((By.CSS_SELECTOR, "span[title='VMC OIC-2 batch 2022']")),
    EC.presence_of_element_located((By.CSS_SELECTOR, "div#startup")),
    EC.presence_of_element_located((By.CSS_SELECTOR, "div#initial-startup"))
))

expected_conditions.none_of(*expected_conditions) expected_conditions.none_of(*expected_conditions)

selenium.webdriver.support.expected_conditions.none_of(*expected_conditions) is the expectation that all of multiple expected conditions is true. selenium.webdriver.support.expected_conditions.none_of(*expected_conditions)是期望所有多个预期条件都为真。 Equivalent to a logical AND .等效于逻辑AND It is defined as:它被定义为:

def none_of(*expected_conditions):
    """ An expectation that none of 1 or multiple expected conditions is true.
    Equivalent to a logical 'NOT-OR'.
    Returns a Boolean """
    def none_of_condition(driver):
    for expected_condition in expected_conditions:
        try:
        result = expected_condition(driver)
        if result:
            return False
        except WebDriverException:
        pass
    return True
    return none_of_condition

An example一个例子

WebDriverWait(browser, 10).until(EC.none of(
    EC.presence_of_element_located((By.CSS_SELECTOR, "span[title='VMC OIC-2 batch 2022']")),
    EC.presence_of_element_located((By.CSS_SELECTOR, "div#startup")),
    EC.presence_of_element_located((By.CSS_SELECTOR, "div#initial-startup"))
))

Note笔记

You have to add the following imports:您必须添加以下导入:

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

This usecase这个用例

As per the CHANGELOGS there was a fix to the all_of() function in Selenium 4.0 Alpha 3 :根据CHANGELOGS ,在Selenium 4.0 Alpha 3中修复了all_of() function :

  • Fixing check of type of a returned element in a test for all_of condition在 all_of 条件的测试中修复返回元素类型的检查

Solution解决方案

Upgrade the Selenium python client version to recent Selenium 4.0 Beta 1Selenium python 客户端版本升级到最新的Selenium 4.0 Beta 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python中使用Selenium的Expected_conditions - expected_conditions with Selenium in Python Selenium的预期条件[Python] - expected_conditions with Selenium [Python] 在python +硒中使用Expected_conditions时出现错误 - Getting error in using expected_conditions in python + selenium selenium.common.exceptions.TimeoutException 错误使用 WebDriverWait 通过 Selenium 和 Python - selenium.common.exceptions.TimeoutException error using WebDriverWait with expected_conditions through Selenium and Python 通过硒和python 3使用element_to_be_clickable方法时,Expected_Conditions返回TimeoutException - Expected_Conditions returns TimeoutException while using element_to_be_clickable method through selenium & python 3 “TypeError: ‘str’ object is not callable” 在 expected_conditions 内通过 Python + Selenium 传递定位器 - "TypeError: 'str' object is not callable" passing locator within expected_conditions through Python + Selenium Selenium:在 Expected_conditions 的等待中为孩子重用已找到的元素 - Selenium: reuse of found elements for child in Wait of Expected_conditions selenium.common.exceptions.TimeoutException 在使用 Selenium Python 中的 expected_conditionspresence_of_element_located 单击按钮时 - selenium.common.exceptions.TimeoutException while clicking on a button using expected_conditions presence_of_element_located in Selenium Python __init __()接受2个位置参数,但是使用Selenium Python使用WebDriverWait和Expected_conditions作为element_to_be_clickable给出了3个 - __init__() takes 2 positional arguments but 3 were given using WebDriverWait and expected_conditions as element_to_be_clickable with Selenium Python Python webdriverwait 或 expected_conditions 对我不起作用 - Python webdriverwait or expected_conditions doesnt work for me
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM