I have a Selenium script in Python. The webpage that I am trying to access will have one of two possible results:
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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With