2

Assuming that I have 3 different scenarios in those different scenarios different elements are shown.

How can I use WebDriverWait for multiple elements and if one of those elements are found ignore the finding of other elements.

I had tried WebDriverWait to sleep for x number of seconds then do an if statement with driver.find_element_by_id and find if the elements are present but this is highly inefficient because the page can take longer/less to load, you can see what I tried in the following code:

WebDriverWait(driver, 30)
if len(driver.find_elements_by_id('something1')) > 0:
    *do something*
elif len(driver.find_element_by_id('something2')) > 0:
    *do something*
elif len(driver.find_element_by_id('something3')) > 0:
    *do something*

I also tried WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, 'something'))) with a try and except but this is the most inefficient method as it because it takes much more longer longer.

3
  • assuming you are ranking them in order of importance from top to bottom, can't you just add a breakstatement under each *do something*? Actually it doesn't need to be in order of importance if all you care about is if ANY element is found. Either way though, I think break should work for this use case Commented May 14, 2021 at 0:19
  • @JD2775 Def not the solution for me, since i have to wait for the page to be loaded first with webdriverwait or through a sleep if not it would raise an error, also break will not work because it is gonna have different code based on the element found. Commented May 14, 2021 at 0:41
  • well you'd need to adjust your find element by to include the wait method wrapped around it. You already have the WebDriverWait(driver, 30) initiated above the if block, just change how you are finding the elements below it to incorporate it. Also, not sure how the "different code based on the element found" matters, the break is going beneath that, in each if/else statement Commented May 14, 2021 at 0:44

1 Answer 1

1

In order to wait until one of three elements is presented you can use the following ExpectedConditions example since
ExpectedConditions supports multiple arguments.
So you can use something like the following:

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "first_element_id, second_element_id, third_element_id"))

Now, if you need to know what element is present and what not, the simplest way to do that is using the driver.find_elements

is_first_element_present = driver.find_elements_by_id(first_element_id)
is_second_element_present = driver.find_elements_by_id(second_element_id)
is_third_element_present = driver.find_elements_by_id(third_element_id)

Now, each of those elements is a list. In case the element is present, the list is not empty and in case the element is absent the list is empty. And since non-empty list is Boolean True in Python and empty list is False you can use it directly with if case:

if(is_first_element_present):
   do_something
elif(is_second_element_present):
   do_that
   -----
etc
Sign up to request clarification or add additional context in comments.

3 Comments

This is a great answer but not a viable solution for me, i need to know what element has been found since it can be any element of the 3. Also it wont cause an exception timeout if 1 or 2 elements are not found?
This is a better solution that i had in mind but im struggling with the part that i dont want to run the if statements if the page is not loaded and i dont want to do a sleep before the statements because it can take longer/less for the page to load. In my mind i was thinking if statements with your first solution but that will wait much longer in an if elif statement. At the end ill just do with sleep this is too much of an headache.
These are opposite scenarios. If you just want to wait for 1 of several elements to appear (and this is what you asked as far as I understood) - use the solution I provided. In case you want to validate the final state of ALL the 3 elements I see no way, only to use a dedicated ExpectedConditions for each element that will return True or False, True if element is found and False when the timeout reached. So, in this case you will wait at least for the entire single timeout period defined within your WebdriverWait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.