Dynamic Locator Strategies

Introduction

Dynamic Locator Strategies are techniques used in Selenium to locate web elements whose attributes, positions, or existence change during runtime. Modern web applications frequently generate elements dynamically using JavaScript, making traditional static locators unreliable.

Dynamic Locator Strategies combine powerful Selenium techniques such as XPath Functions, XPath Axes, partial attribute matching, and text-based locators to reliably identify dynamically generated elements.

In professional automation frameworks, Dynamic Locator Strategies play a crucial role in reducing test failures caused by frequently changing user interfaces and dynamically loaded content.

In this tutorial, you’ll learn what Dynamic Locator Strategies are, why they are used, commonly used techniques, practical examples, real-world use cases, common mistakes, and best practices.


What are Dynamic Locator Strategies?

Dynamic Locator Strategies are approaches used to locate web elements whose attributes or positions may change during execution.

Instead of depending upon static IDs or deeply nested HTML structures, Selenium uses flexible locator techniques to identify elements dynamically at runtime.

For example, consider the following HTML:

<button>
    Add Element
</button>

<button
    class="added-manually">
    Delete
</button>

The dynamically created Delete button can be located using:

//button[contains(@class,'added-manually')]

This XPath expression searches for any button whose class attribute contains added-manually, making the locator flexible and maintainable.


Why Use Dynamic Locator Strategies?

Dynamic Locator Strategies are useful because they:

  • Support locating dynamically generated web elements.

  • Improve locator flexibility and maintainability.

  • Reduce automation failures caused by UI changes.

  • Handle partially changing attribute values efficiently.

  • Improve framework scalability.

  • Are widely used in professional Selenium automation frameworks.


Common Dynamic Locator Techniques

1. Using contains()

driver.find_element(
    By.XPATH,
    "//button[contains(@class,'value')]"
)

2. Using text()

driver.find_element(
    By.XPATH,
    "//button[text()='Login']"
)

3. Using starts-with()

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

4. Combining XPath Functions and Axes

driver.find_element(
    By.XPATH,
    "//label/following-sibling::input"
)

These techniques provide flexible and maintainable solutions for locating dynamic web elements.


Example

The Selenium practice website dynamically generates a Delete button whenever the Add Element button is clicked. Dynamic Locator Strategies allow Selenium to locate the newly created element reliably.

The Selenium code is:

from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: 13. XPath - Dynamic Locator Strategies
# Practice site: https://the-internet.herokuapp.com/add_remove_elements/
# Run: pytest -s 13_examples/test_07_dynamic_locator_strategies.py
#
# Combine XPath functions with axes to locate dynamically added elements that
# do not have stable IDs.


def test_dynamic_locator_strategies():
    driver = webdriver.Chrome()

    try:
        driver.get(
            "https://the-internet.herokuapp.com/add_remove_elements/"
        )

        driver.find_element(
            By.XPATH,
            "//button[text()='Add Element']"
        ).click()

        delete_button = driver.find_element(
            By.XPATH,
            "//button[contains(@class, 'added-manually')]",
        )

        assert delete_button.is_displayed()

    finally:
        driver.quit()

Output

The Delete button is created
dynamically after clicking
Add Element and is located
successfully using Dynamic
Locator Strategies.

Understanding the Code

Import the By Class

from selenium.webdriver.common.by import By

Imports Selenium’s locator strategies.

Open the Practice Website

driver.get(
    "https://the-internet.herokuapp.com/add_remove_elements/"
)

Launches the Selenium practice website.

Click the Add Element Button

driver.find_element(
    By.XPATH,
    "//button[text()='Add Element']"
).click()

Creates a new Delete button dynamically during execution.

Locate the Dynamically Generated Element

driver.find_element(
    By.XPATH,
    "//button[contains(
        @class,
        'added-manually'
    )]"
)

Uses the contains() function to locate the dynamically generated Delete button using its class attribute.

Validate the Element

assert delete_button.is_displayed()

Verifies that the dynamically created Delete button is visible on the webpage.


How Dynamic Locator Strategies Work

          Python Script
                 │
                 ▼
              By.XPATH
                 │
                 ▼
      Perform User Interaction
                 │
                 ▼
      Dynamically Generate Element
                 │
                 ▼
       Apply Dynamic XPath Strategy
                 │
                 ▼
         Locate Matching Element
                 │
                 ▼
            Perform Validation

Practical Example

Suppose you’re automating an E-Commerce website.

Clicking the Add to Cart button dynamically creates a Checkout button that was not initially present on the webpage.

Instead of relying upon static locators, Selenium uses:

//button[contains(@class,'checkout')]

to locate the dynamically generated Checkout button successfully.

This significantly improves locator flexibility and reliability.


Automation Testing Example

Consider an online banking application.

After successful authentication, the application dynamically generates account information panels and transaction buttons.

Selenium uses Dynamic Locator Strategies such as:

  • contains()

  • starts-with()

  • text()

  • XPath Axes

to identify dynamically generated elements during regression testing across multiple environments.


Real-World Example

Automation engineers frequently use Dynamic Locator Strategies while working with:

  • Banking applications.

  • E-Commerce websites.

  • Healthcare portals.

  • CRM systems.

  • ERP applications.

  • SaaS products.

  • Enterprise web applications.

  • Dynamic dashboards.

  • Single Page Applications (SPAs).

These strategies are particularly useful whenever elements are created, removed, or modified dynamically during execution.


Advantages of Dynamic Locator Strategies

  • Support locating dynamically generated elements.

  • Improve framework maintainability.

  • Reduce automation failures caused by UI changes.

  • Improve locator flexibility.

  • Produce reusable XPath expressions.

  • Widely used in professional Selenium automation frameworks.


Limitations of Dynamic Locator Strategies

  • Complex XPath expressions may reduce readability.

  • Incorrect partial matching may return unintended elements.

  • Dynamic webpages may still require synchronization techniques.

  • Poorly designed locators can reduce framework maintainability.


Common Mistakes Beginners Make

Depending Upon Static Attributes

Avoid

//button[@id='delete_125634']

Prefer

//button[contains(
    @class,
    'added-manually'
)]

Partial attribute matching significantly improves locator reliability.

Ignoring Dynamic Content

Always determine whether elements are generated dynamically before selecting a locator strategy.

Writing Overly Complex XPath Expressions

Keep XPath expressions simple, readable, and maintainable whenever possible.


Best Practices

  • Prefer partial attribute matching whenever appropriate.

  • Use contains() and starts-with() for dynamic attributes.

  • Combine XPath Functions and XPath Axes when required.

  • Verify locators using browser Developer Tools.

  • Keep XPath expressions simple and reusable.

  • Write maintainable locators within automation frameworks.

  • Use appropriate synchronization techniques when working with dynamic elements.


Conclusion

Dynamic Locator Strategies provide flexible and maintainable solutions for locating dynamically generated web elements in modern web applications. By combining XPath Functions, partial attribute matching, and XPath Axes, Selenium can reliably identify elements whose attributes or existence change during execution.

Understanding Dynamic Locator Strategies is an essential skill for building scalable and reliable Selenium automation frameworks used in professional software testing environments.


Frequently Asked Questions (FAQs)

What are Dynamic Locator Strategies?

Dynamic Locator Strategies are techniques used to locate web elements whose attributes or positions change dynamically during execution.

Which techniques are commonly used for Dynamic Locator Strategies?

The most commonly used techniques include:

  • contains()

  • starts-with()

  • text()

  • XPath Axes

  • Partial attribute matching

Why are Dynamic Locator Strategies useful?

They improve locator flexibility, reduce automation failures, and provide maintainable solutions for dynamic web applications.

Are Dynamic Locator Strategies useful for modern web applications?

Yes.

They are extensively used while automating Single Page Applications (SPAs), dynamic dashboards, and JavaScript-based web applications.

When should I use Dynamic Locator Strategies?

Use them whenever web elements are dynamically created, removed, or modified during execution.


Key Takeaways

  • Dynamic Locator Strategies provide flexible solutions for locating dynamic web elements.

  • XPath Functions and XPath Axes are commonly used while creating dynamic locators.

  • Partial attribute matching significantly improves locator maintainability.

  • Prefer reusable and readable XPath expressions whenever possible.

  • Use appropriate synchronization techniques when working with dynamic content.

  • Dynamic Locator Strategies are widely used in professional Selenium automation frameworks.