All Courses
Scraping

How to Handle Dynamic Elements in Selenium

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:

  • Appear after a delay due to AJAX calls or JavaScript execution.
  • Have properties (like IDs or class names) that change with each page load.
  • Depend on user interaction to become visible or active.

Handling such elements requires a strategy to ensure they are interactable before Selenium attempts to interact with them.

Waiting for Elements

Selenium provides two primary types of waits to handle timing issues when dealing with dynamic elements: implicit and explicit waits.

Implicit 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

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()

Common Expected Conditions:

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.

Combining Implicit and Explicit 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.

Join Our Community!

Our community is here to support your growth, so why wait? Join now and let’s build together!

ArrowArrow
Try Rayobyte proxies for all your scraping needs
Explore Now

See What Makes Rayobyte Special For Yourself!