Home > Software engineering >  Is it possible to use an OR statement within WebDriverWait?
Is it possible to use an OR statement within WebDriverWait?

Time:09-28

Is it possible to use an OR statement with WebDriverWait? Basically I want the code to look for a UPC product code OR a Not Found statement. Only one or the other will show up on the website, so as soon as it finds either I want to to do something. If it finds the UPC I want it to click on it. If it finds the Not Found statement, I want it to append the UPC that wasn't found to a list. Below is the snippet of code from my larger For Loop. I'd prefer not to wait for the 10 with an else statement, but sometimes the search function on the website does load slower, so it's nice to have that larger 10 timer.

WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="' upc '"]' OR '/html/body/app-root/div/crf-report-route-entry/crf-report-page/div/div[2]/crf-data-selector-panel/crf-navigation-list/div[2]/p')))

CodePudding user response:

There is any_of expected_condition.
As per documentation:

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.

The example of use of it looks like:

WebDriverWait(driver, 20).until(
    EC.any_of(
        EC.visibility_of_element_located((By.ID, "id1")),
        EC.visibility_of_element_located((By.ID, "id2"))
    )
)

UPD
Alternatively you can try using a python's lambda expression, using the find_elements methods glued together with an or operator as following:

wait.until(lambda x: x.find_elements(By.ID, "id1") or x.find_elements(By.ID, "#id2"))

Read this answer for more details

  • Related