Does mixing Implicit and Explicit waits cause inconsistent timeout behavior?
0 reputation · 28 May 2020, 07:53 UTC
0 reputation · 28 May 2020, 07:53 UTC
Selenium WebDriver provides two primary mechanisms for handling asynchronous element loading: Implicit waits, which apply globally to the driver session, and Explicit waits (WebDriverWait), which target specific elements using ExpectedConditions.
While Explicit waits are generally preferred for precision, there is uncertainty regarding the internal interaction between these two mechanisms when active simultaneously. Specifically, the polling logic of a WebDriverWait may conflict with the global timeout of an Implicit wait, potentially leading to unpredictable delays that exceed the defined maximum timeout.
34775 reputation · 28 May 2020, 10:00 UTC
Yes. When an ImplicitWait is set on the driver and a WebDriverWait (explicit wait) is used for an element, the two mechanisms can interact in a way that makes the overall timeout longer than expected or inconsistent across driver implementations.
Implicit waits are essentially a blanket retry mechanism: the driver will retry locating an element for the configured period. Explicit waits wrap a condition and poll the page at a configurable interval. When both are active, the implicit retry can “steal” time from the explicit wait’s polling loop. For example, with a 10‑second implicit wait and a 10‑second explicit wait on a missing element, the first lookup may block for 10 seconds, then the explicit wait starts its own polling cycle, potentially adding another 10 seconds before a TimeoutException is thrown.
import time
start = time.time()
try:
driver.implicitly_wait(10)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'missing')))
except TimeoutException:
pass
print(f"Elapsed: {time.time() - start:.2f}s")
driver.implicitly_wait(0)
To fine‑tune the recommendation, we need to know which WebDriver implementation and version you are using. Different drivers implement the polling loop differently, which can change the observed timeout behavior.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.