[英]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.我正在使用
WebDriverWait
和selenium.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
https://github.com/SeleniumHQ/selenium/releases https://github.com/SeleniumHQ/selenium/releases
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 版本中交付。
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
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"))
))
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
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"))
))
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
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"))
))
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
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 条件的测试中修复返回元素类型的检查
Upgrade the Selenium python client version to recent Selenium 4.0 Beta 1将Selenium python 客户端版本升级到最新的Selenium 4.0 Beta 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.