I am using Selenium with Python and came across the need to use WebDriverWait
and selenium.webdriver.support.expected_conditions
in 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:
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.
Another thing to note is that the mentioned conditions are present in Selenium with 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).
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.
Thanks for reading!
pip install selenium==4.0.0.a7
https://github.com/SeleniumHQ/selenium/releases
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.
selenium.webdriver.support.expected_conditions.all_of(*expected_conditions) is the expectation that all of multiple expected conditions is true. Equivalent to a logical 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. Equivalent to a logical 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. Equivalent to a logical 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
:
- Fixing check of type of a returned element in a test for all_of condition
Upgrade the Selenium python client version to recent Selenium 4.0 Beta 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.