Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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!

like image 390
ankurbohra04 Avatar asked Sep 19 '25 20:09

ankurbohra04


1 Answers

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. 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

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)

selenium.webdriver.support.expected_conditions.any_of(*expected_conditions) is the expectation that any of multiple expected conditions is true. Equivalent to a logical OR. 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)

selenium.webdriver.support.expected_conditions.none_of(*expected_conditions) is the expectation that none of 1 or multiple expected conditions is true. Equivalent to a logical NOT-OR. 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:

  • Fixing check of type of a returned element in a test for all_of condition

Solution

Upgrade the Selenium python client version to recent Selenium 4.0 Beta 1

like image 112
undetected Selenium Avatar answered Sep 22 '25 10:09

undetected Selenium