Dynamic XPath

Introduction

Dynamic XPath is one of the most powerful XPath techniques used in Selenium for locating web elements whose attributes change partially or dynamically during runtime. Unlike static XPath expressions, Dynamic XPath uses XPath functions such as contains(), starts-with(), and text() to identify elements more flexibly.

Modern web applications frequently generate dynamic IDs, classes, and attribute values, making traditional locators unreliable. Dynamic XPath provides a maintainable and scalable approach for locating such elements in automation projects.

In this tutorial, you’ll learn what Dynamic XPath is, why it is used, different Dynamic XPath techniques, practical examples, real-world use cases, common mistakes, and best practices.


What is Dynamic XPath?

Dynamic XPath is an XPath expression that uses XPath functions to locate web elements whose attributes or values may change partially during execution.

Instead of depending upon an exact attribute value, Dynamic XPath locates elements using partial matches or visible text.

For example, consider the following HTML:

<a href="/login">
    Form Authentication
</a>

The hyperlink can be located using:

//a[contains(@href,'/login')]

Dynamic XPath searches for elements whose href attribute contains the specified value rather than requiring an exact match.


Why Use Dynamic XPath?

Dynamic XPath is useful because it:

  • Supports locating dynamic web elements.

  • Handles partially changing attribute values.

  • Produces flexible and maintainable locators.

  • Improves framework reliability.

  • Reduces automation failures caused by UI changes.

  • Is widely used in professional Selenium automation frameworks.


Syntax

Using contains()

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

Using starts-with()

driver.find_element(
    By.XPATH,
    "//tagname[starts-with(@attribute,'value')]"
)

Using text()

driver.find_element(
    By.XPATH,
    "//tagname[text()='Visible Text']"
)

Types of Dynamic XPath

1. contains()

Locates elements whose attribute values contain a specific text.

//a[contains(@href,'/login')]

2. starts-with()

Locates elements whose attribute values begin with a specific text.

//input[starts-with(@id,'user')]

3. text()

Locates elements using their visible text.

//button[text()='Login']

These functions significantly improve the flexibility of XPath expressions when working with modern web applications.


Example

The Selenium practice website contains a hyperlink whose href attribute includes /login. Dynamic XPath uses the contains() function to identify the required hyperlink successfully.

The Selenium code is:

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


# Topic: 13. XPath - Dynamic XPath
# Practice site: https://the-internet.herokuapp.com/
# Run: pytest -s 13_examples/test_04_dynamic_xpath.py
#
# Dynamic XPath uses functions like contains() and starts-with() to match
# elements whose attributes change partially.


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

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

        link = driver.find_element(
            By.XPATH,
            "//a[contains(@href, '/login')]",
        )

        assert "Form Authentication" in link.text

    finally:
        driver.quit()

Output

The Form Authentication
hyperlink is located
successfully using the
contains() function in
Dynamic XPath.

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/"
)

Launches the Selenium practice website.

Locate the Hyperlink

driver.find_element(
    By.XPATH,
    "//a[contains(@href,'/login')]"
)

Uses the contains() function to locate the hyperlink whose href attribute partially matches /login.

Validate the Element

assert "Form Authentication" in link.text

Verifies that the located hyperlink contains the expected visible text.


How Dynamic XPath Works

        Python Script
               │
               ▼
            By.XPATH
               │
               ▼
      Read XPath Expression
               │
               ▼
        Apply XPath Function
               │
               ▼
       Perform Partial Matching
               │
               ▼
      Locate Matching Element
               │
               ▼
         Perform Validation

Practical Example

Suppose you’re automating a webpage that generates dynamic IDs during every execution.

Instead of using:

//input[@id='user_123456']

Selenium can use:

//input[starts-with(@id,'user')]

This makes the automation script significantly more reliable when attribute values change dynamically.


Automation Testing Example

Consider an online banking application.

The Login button’s ID changes during every deployment, but its visible text remains unchanged.

Instead of locating the button using its dynamic ID, Selenium uses:

//button[text()='Login']

to perform authentication-related validations during regression testing.


Real-World Example

Automation engineers frequently use Dynamic XPath while working with:

  • Banking applications.

  • E-Commerce websites.

  • Healthcare portals.

  • CRM systems.

  • ERP applications.

  • Enterprise web applications.

  • SaaS products.

  • Dynamic dashboards.

Dynamic XPath is particularly useful whenever applications generate partially changing attribute values during runtime.


Advantages of Dynamic XPath

  • Supports locating dynamic web elements.

  • Handles partially changing attributes efficiently.

  • Improves framework maintainability.

  • Provides flexible element identification.

  • Reduces automation failures caused by UI changes.

  • Widely used in professional Selenium automation frameworks.


Limitations of Dynamic XPath

  • Complex XPath expressions can reduce readability.

  • Incorrect partial matching may return multiple elements.

  • Dynamic webpage structures may still affect locator reliability.

  • Overusing XPath functions can make scripts difficult to maintain.


Common Mistakes Beginners Make

Using Exact Matches for Dynamic Elements

Avoid

//input[@id='user_145893']

Prefer

//input[starts-with(@id,'user')]

Using partial matches significantly improves locator reliability.

Writing Overly Complex XPath Expressions

Keep XPath expressions short and readable whenever possible.

Ignoring Simpler Locator Strategies

Before using Dynamic XPath, always check whether ID, Name, or CSS Selectors can uniquely identify the required element.


Best Practices

  • Prefer Dynamic XPath for partially changing attributes.

  • Use contains() and starts-with() whenever appropriate.

  • Keep XPath expressions simple and readable.

  • Verify XPath expressions using browser Developer Tools.

  • Avoid depending upon deeply nested HTML structures.

  • Use stable locator strategies whenever possible.

  • Write maintainable and reusable XPath expressions within automation frameworks.


Conclusion

Dynamic XPath is one of the most powerful XPath techniques in Selenium automation. It provides flexible and maintainable solutions for locating dynamic web elements whose attributes change partially during runtime.

Understanding Dynamic XPath significantly improves your ability to automate modern web applications and provides the foundation for learning advanced XPath Functions and XPath Axes used extensively in professional automation frameworks.


Frequently Asked Questions (FAQs)

What is Dynamic XPath?

Dynamic XPath uses XPath functions such as contains(), starts-with(), and text() to locate web elements whose attributes or values may change dynamically.

What is the syntax for Dynamic XPath?

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

Why is Dynamic XPath useful?

It helps locate dynamic web elements whose attribute values change partially during execution.

Which XPath functions are commonly used in Dynamic XPath?

The most commonly used functions include:

  • contains()

  • starts-with()

  • text()

When should I use Dynamic XPath?

Use Dynamic XPath whenever applications contain dynamic IDs, partially changing attributes, or complex web elements that cannot be reliably located using simpler locator strategies.


Key Takeaways

  • Dynamic XPath uses XPath functions to locate dynamic web elements.

  • contains(), starts-with(), and text() are the most commonly used Dynamic XPath techniques.

  • Dynamic XPath significantly improves locator flexibility and maintainability.

  • Prefer partial attribute matching whenever element attributes change dynamically.

  • Keep XPath expressions simple, readable, and reusable.

  • Dynamic XPath is widely used in professional Selenium automation frameworks.