Logic Operators in WebDriverWait Expected Conditions

3.1k views Asked by At

I'm using Python / Selenium to submit a form then I have the web driver waiting for the next page to load by using an expected condition using class id.

My problem is that there are two pages that can be displayed but they do not share an unique element (that I can find) that is not in the original page. One page has a unique class is of mobile_txt_holder and the other possible page has a class id of notfoundcopy. I would like to use a wait that is looking for mobile_txt_holder OR notfoundcopy to appear.

Is it possible to combine two expected conditions into one wait?

Basic idea of what I am looking for but obviously won't work:

WebDriverWait(driver, 30).until(EC.presence_of_element_located(
    (By.CLASS_NAME, "mobile_txt_holder")))
    or .until(EC.presence_of_element_located((By.CLASS_NAME, "notfoundcopy")))

I really just need to program to wait until the next page loads so that I can parse the source.

Sample HTML:

<p class="notfoundcopy">Unfortunately, the number you entered is not in our tracking system.</p>

2

There are 2 answers

2
undetected Selenium On BEST ANSWER

Apart from clubbing up 2 expected_conditions through or clause, we can easily construct a CSS to take care of our requirement The following CSS will look either for the EC either in mobile_txt_holder class or in notfoundcopy class:

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, ".mobile_txt_holder, .notfoundcopy"))

You can find a detailed discussion in selenium two xpath tests in one

0
Ryan Nowakowski On

Since selenium python v4.0.0, you can use EC.any_of to look for multiple conditions or'd together:

WebDriverWait(driver, 30).until(
    EC.any_of(
        EC.presence_of_element_located((By.CLASS_NAME, "mobile_txt_holder")),
        EC.presence_of_element_located((By.CLASS_NAME, "notfoundcopy"))
    )
)