Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium wait for either of 2 conditions to be present

I have a Selenium script in Python. The webpage that I am trying to access will have one of two possible results:

  1. Either, it won't have the required data, in which case it will print out a certain text (for example, "data not found")
  2. Or, it will have the required data and load it into an element that I can access by ID (say, "dataID")

I need to wait for either of these conditions to be true. If any of these conditions is met, I will simply extract the page source and quit.

I can separately wait for these conditions as follows:

element1 = WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.ID, "nodataID"), "data not found"))
if element1:
    content = driver.page_source.encode('utf-8')
    driver.quit()

element2 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "dataID")))
if element2:
    content = driver.page_source.encode('utf-8')
    driver.quit()

This means that Selenium will first wait to see if the webpage does not have the required data. In that case, it will get page source and quit. But if this condition is not met, then it will continue to wait for another 10 seconds to see if the second condition is met.

The problem with this approach is that I am not sure that the first condition, if true, will be met in 10 seconds. That is, I might wait for 10 seconds and if the first condition is not met, then I will move one to look for the second condition. But it might be the case that the first condition is actually true, and it got realized in the 11th second. In such a case, my script will never know that the first condition was realized. If I switch the order of the two wait statements, then the issue still remains, because now I'm still not sure for how long to wait for the new first condition to be true.

Another problem with this approach is that it will wait for 10 seconds for the first condition to be met even if the second condition is actually met in just 2-3 seconds. So, there is wasted waiting time when the second condition is true for the webpage.

So, I need to be able to write a wait that waits for either of the two conditions to be true.

I reviewed several other Stackoverflow questions and tried to come up with a solution, but I could not solve it. I tried combining the 2 conditions with a comma (,) or a || or an OR keyword. But none of these solve the problem.

Can someone please point me in the right direction?

like image 418
Tapa Dipti Sitaula Avatar asked Feb 11 '17 22:02

Tapa Dipti Sitaula


1 Answers

You can use CSS_SELECTOR or to wait for one of the conditions

WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, "#nodataID, #dataID")))
content = driver.page_source.encode('utf-8')
driver.quit()

You also don't need the if element check. If the expected condition will fail it will throw TimeoutException.

like image 199
Guy Avatar answered Oct 23 '22 05:10

Guy