WebDriver provides two primary waiting strategies to handle dynamic content loading: implicit and explicit waits.
Implicit Wait
An implicit wait is a global setting applied to the WebDriver instance. Once configured, it instructs the driver to poll the DOM for a specified duration (default polling interval is 0.5 seconds) when trying to locate an element that isn't immediately avaialble. This wait is enforced on the server side and remains active for the entire lifetime of the WebDriver session.
However, implicit waits only verify the presence of a element in the DOM. They cannot validate advanced states such as visibility, clickability, or enabled status.
Explicit Wait
In contrast, explicit waits are client-side conditions applied selectively at specific points in the code. They allow fine-grained control by waiting for a custom condition to become true before proceeding. These waits are implemented using WebDriverWait in combination with expected conditions or custom lambda expressions.
The core logic of until() in WebDriverWait repeatedly invokes a provided method (e.g., an element locator) until it returns a truthy value or the timeout expires:
def until(self, method, message=''):
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message, screen, stacktrace)
Key parameters include:
_poll: Polling frequency (defaults to 0.5 seconds)._ignored_exceptions: Exceptions to ignore during polling (typicallyNoSuchElementException).
A common usage pattern employs a lambda to define the condition:
element = WebDriverWait(driver, 10).until(
lambda d: d.find_element(By.ID, "someId")
)
This approach returns the located element once found, enabling further interacsions like clicking or text validation—capabilities beyond the scope of implicit waits.
Additionally, mobile automation frameworks often extend standard locators (e.g., Selenium’s By) to support platform-specific attributes, such as accessibility IDs or iOS predicates, through specialized classes like MobileBy.