Now you know how to find appropriate elements, it’s time to learn how to handle dynamic elements in Selenium, too. Specifically, dynamic elements can be defined as elements that:
Handling such elements requires a strategy to ensure they are interactable before Selenium attempts to interact with them.
Selenium provides two primary types of waits to handle timing issues when dealing with dynamic elements: implicit and explicit waits.
An implicit wait tells Selenium to wait for a specified amount of time when trying to locate an element before throwing an exception.
Once set, the implicit wait applies globally to all find_element
and find_elements
calls.
from selenium import webdriver
driver = webdriver.Chrome()
# Set an implicit wait of 10 seconds
driver.implicitly_wait(10)
driver.get("https://example.com")
element = driver.find_element("id", "dynamicElementId")
# Perform actions
print(element.text)
driver.quit()
The benefit of implicit waits is that they are simpler to use and are applied globally. However, they can load to unnecessary delays if all the elements load faster than the assigned wait time.
Explicit waits allow you to wait for a specific condition to be met before proceeding. They are more targeted and flexible than implicit waits.
Explicit waits are implemented using WebDriverWait
in combination with expected conditions from the selenium.webdriver.support.expected_conditions
module. We’ll take a look at expected conditions shortly, but here’s how the general explicit wait looks:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://example.com")
# Wait for the element to be visible
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, "dynamicElementId")))
# Perform actions
print(element.text)
driver.quit()
When using explicit waits, you also need to use expected conditions. These represent criteria that elements need to meet as part of the process.
presence_of_element_located
: Ensures the element is present in the DOM.visibility_of_element_located
: Ensures the element is visible.element_to_be_clickable
: Ensures the element is visible and enabled for clicking.Overall, explicit waits are highly precise and have the added benefit of avoiding unnecessary delays. However, this comes at the cost of requiring more code and specific requirements than implicit waits.
While implicit and explicit waits can be combined, it is generally discouraged because implicit waits apply globally and can interfere with explicit wait timing. It's better to choose one strategy for any given script.
Our community is here to support your growth, so why wait? Join now and let’s build together!