为什么Selenium python包中不存在所有、无和任何预期条件?
我将Selenium与Python结合使用,并发现需要在其中使用为什么Selenium python包中不存在所有、无和任何预期条件?,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我将Selenium与Python结合使用,并发现需要在其中使用WebDriverWait。我必须等待一个元素被删除并显示另一个元素,因此我希望使用all_of和none_of使条件更容易理解: WebDriverWait(浏览器,10)。直到( 欧共体( EC.元素的存在(通过.CSS选择器,“span[title='VMC OIC-2批次2022'”), 欧共体( EC.元素的存在(通过.CSS选择器,“div#startup”), EC.元素的存在位置((By.CSS选择器,“div#初始
WebDriverWait
。我必须等待一个元素被删除并显示另一个元素,因此我希望使用all_of和none_of使条件更容易理解:
WebDriverWait(浏览器,10)。直到(
欧共体(
EC.元素的存在(通过.CSS选择器,“span[title='VMC OIC-2批次2022'”),
欧共体(
EC.元素的存在(通过.CSS选择器,“div#startup”),
EC.元素的存在位置((By.CSS选择器,“div#初始启动”))
)
)
)
不幸的是,我收到了这个错误:
AttributeError: module 'selenium.webdriver.support.expected_conditions' has no attribute 'all_of'
通过未经加工的印刷品进行进一步检查,我知道任何、所有和任何条件都不起作用
另一件需要注意的事情是,上述条件在Selenium with Java中存在。我可以发现,在谈到这一点时,似乎所有的、所有的和没有的最终都添加到了预期的条件中,这在Selenium存储库的相应文件中是显而易见的:(最后三个函数)
这个问题需要一个解释,而不是一个替代方案,我很清楚我可以编写一个函数来完成相同的任务,甚至可以复制expected_conditions.py中列出的函数,但我真的很想知道为什么这些函数存在于存储库中而不存在于工作包中
感谢阅读!本文介绍了它们。虽然中的版本为3.141,但提交可能会在4.0.0版本中提供
pip install selenium==4.0.0.a7
安装selenium 4 alpha版本,selenium 3.141中将不会有新的更新
您可以看到v3的上一次更新是在2018年12月。单击“查看新标签”查看最新的v4版本
本文件按照selenium 4更新
预期条件。所有(*预期条件)
是所有多个预期条件均为真的预期。相当于逻辑的和
。其定义为:
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
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
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
一个例子
预期条件。任何(*预期条件) 是所有多个预期条件均为真的预期。相当于逻辑的
和
。其定义为:
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
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
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
一个例子
预期条件。无(*预期条件) 是所有多个预期条件均为真的预期。相当于逻辑的
和
。其定义为:
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
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
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
一个例子
注 您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
这个用例 根据Selenium 4.0 Alpha 3中的
all_of()
函数已修复:
- 修复所有条件测试中返回元素类型的检查
解决方案 将python客户端版本升级到最新版本