How to Handle Dynamic Web Elements in Selenium with Python

Handling dynamic web elements in Selenium with Python is a common challenge in test automation. Dynamic elements are those whose attributes (like IDs, classes, or XPaths) change every time the page reloads or user interactions occur.

How to Handle Dynamic Web Elements in Selenium with Python

Here’s a step-by-step guide on how to effectively handle them:


πŸ” 1. Use Dynamic XPath or CSS Selectors

Instead of relying on exact IDs, use partial matches or attribute-based locators.


✅ Examples:

python

Copy

Edit

# XPath using 'contains'

driver.find_element(By.XPATH, "//*[contains(@id, 'dynamic_part')]")


# XPath using 'starts-with'

driver.find_element(By.XPATH, "//*[starts-with(@id, 'prefix')]")


# CSS Selector with attribute

driver.find_element(By.CSS_SELECTOR, "[class*='partial-class-name']")

⏳ 2. Use Explicit Waits

Dynamic elements might take time to appear. Avoid time.sleep() and use WebDriverWait.


✅ Example:

python

Copy

Edit

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


wait = WebDriverWait(driver, 10)

element = wait.until(EC.presence_of_element_located((By.ID, "dynamic_id")))

Common expected_conditions:


presence_of_element_located


visibility_of_element_located


element_to_be_clickable


πŸ” 3. Wait for Element to Stabilize (if it changes states)

If an element appears, disappears, or moves, wait for its final state:


python

Copy

Edit

element = wait.until(EC.visibility_of_element_located((By.XPATH, "//button[text()='Submit']")))

πŸ”„ 4. Handle StaleElementReferenceException

If an element reloads after being located, use a try-except block to re-locate it:


python

Copy

Edit

from selenium.common.exceptions import StaleElementReferenceException


try:

    element.click()

except StaleElementReferenceException:

    element = driver.find_element(By.ID, "dynamic_id")

    element.click()

🧩 5. Use JavaScript if Needed

Sometimes, dynamic elements are better handled using JavaScript:


python

Copy

Edit

driver.execute_script("arguments[0].click();", element)

🧠 6. General Tips

Prefer relative XPath over absolute.


Combine multiple attributes in XPath for better accuracy.


Use get_attribute() to debug changing element properties.


✅ Sample Scenario

python

Copy

Edit

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 = WebDriverWait(driver, 10)

element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Continue')]")))

element.click()

 Learn Selenium Python Training in Hyderabad

Read More

Using Browser Developer Tools to Improve Selenium Test Accuracy

Automating PDF Download and Verification Using Selenium and Python

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions


Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing

A Beginner's Guide to ETL Testing: What You Need to Know