XPath Axes

Introduction

XPath Axes are one of the most powerful features provided by XPath in Selenium. They allow automation engineers to navigate through relationships between HTML elements instead of locating elements directly using their attributes or text values.

XPath Axes are particularly useful when a target element does not have unique attributes but can be identified relative to another known element. They make it possible to move between parent, child, sibling, ancestor, and descendant elements within the HTML Document Object Model (DOM).

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


What are XPath Axes?

XPath Axes are special navigation techniques used to locate web elements based on their relationship with other elements present in the HTML document.

Instead of searching the entire webpage, XPath Axes allow Selenium to navigate relative to a known element.

For example, consider the following HTML:

<label for="username">
    Username
</label>

<input
    id="username"
    type="text">

The Username textbox can be located using:

following-sibling::input

which tells Selenium to move from the <label> element to its sibling <input> element.

XPath Axes significantly improve locator flexibility when working with complex HTML structures.


Why Use XPath Axes?

XPath Axes are useful because they:

  • Navigate between related HTML elements.

  • Support locating complex web elements.

  • Improve locator flexibility.

  • Reduce dependency on dynamic attributes.

  • Improve framework maintainability.

  • Are widely used in professional Selenium automation frameworks.


Syntax

Using following-sibling

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

Using parent

driver.find_element(
    By.XPATH,
    "//input/parent::*"
)

Using child

driver.find_element(
    By.XPATH,
    "//form/child::input"
)

Commonly Used XPath Axes

1. following-sibling

Locates sibling elements that appear after the current element.

following-sibling::input

2. parent

Moves from a child element to its parent element.

parent::*

3. child

Locates child elements present within a parent element.

child::input

4. ancestor

Locates all ancestor elements of the current element.

ancestor::form

5. descendant

Locates all descendant elements present within an element.

descendant::input

XPath Axes provide powerful navigation capabilities when working with complex and dynamic webpages.


Example

The Login page contains a Username label and its corresponding Username textbox. XPath Axes allow Selenium to navigate from the known label element to its related input field.

The Selenium code is:

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


# Topic: 13. XPath - XPath Axes
# Practice site: https://the-internet.herokuapp.com/login
# Run: pytest -s 13_examples/test_06_xpath_axes.py
#
# XPath axes like following-sibling and parent navigate relative to a known
# element instead of searching the entire document.


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

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

        username_label = driver.find_element(
            By.XPATH,
            "//label[@for='username']"
        )

        username_input = username_label.find_element(
            By.XPATH,
            "following-sibling::input"
        )

        assert username_input.get_attribute("id") == "username"

    finally:
        driver.quit()

Output

The Username textbox is
located successfully by
navigating from its related
label element using XPath
Axes.

Understanding the Code

Import the By Class

from selenium.webdriver.common.by import By

Imports Selenium’s locator strategies.

Open the Login Page

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

Launches the Selenium practice website.

Locate the Username Label

driver.find_element(
    By.XPATH,
    "//label[@for='username']"
)

Locates the Username label using its for attribute.

Locate the Username Textbox

username_label.find_element(
    By.XPATH,
    "following-sibling::input"
)

Uses the following-sibling axis to navigate from the Username label to its corresponding input field.

Validate the Element

assert username_input.get_attribute(
    "id"
) == "username"

Verifies that the located element contains the expected id attribute value.


How XPath Axes Work

         Python Script
                │
                ▼
             By.XPATH
                │
                ▼
        Locate Known Element
                │
                ▼
          Apply XPath Axis
                │
                ▼
       Navigate HTML Structure
                │
                ▼
         Locate Target Element
                │
                ▼
           Perform Validation

Practical Example

Suppose you’re automating a Registration page.

The Email textbox does not contain a unique ID, but its corresponding label contains a unique attribute.

Instead of locating the Email textbox directly, Selenium can first locate the label and then navigate to the textbox using:

following-sibling::input

This significantly improves locator flexibility and reliability.


Automation Testing Example

Consider an online banking application.

The Login page contains multiple textboxes whose IDs are dynamically generated during deployment.

Instead of depending upon dynamic attributes, Selenium uses XPath Axes to navigate from stable labels or parent containers to the required elements during regression testing.

This approach significantly improves framework maintainability across environments.


Real-World Example

Automation engineers frequently use XPath Axes while working with:

  • Banking applications.

  • E-Commerce websites.

  • Healthcare portals.

  • CRM systems.

  • ERP applications.

  • SaaS products.

  • Enterprise web applications.

  • Dynamic dashboards.

XPath Axes are particularly useful whenever elements can be reliably identified through their relationships with other HTML elements.


Advantages of XPath Axes

  • Navigate between related HTML elements.

  • Improve locator flexibility.

  • Support locating complex web elements.

  • Reduce dependency on dynamic attributes.

  • Improve framework maintainability.

  • Widely used in professional Selenium automation frameworks.


Limitations of XPath Axes

  • Complex XPath expressions may reduce readability.

  • Incorrect navigation may return unintended elements.

  • Deeply nested HTML structures can make locators difficult to maintain.

  • Overusing XPath Axes may unnecessarily complicate automation scripts.


Common Mistakes Beginners Make

Searching the Entire Webpage Unnecessarily

Avoid

//input[@type='text']

Prefer

following-sibling::input

when a related element already uniquely identifies the required element.

Ignoring Element Relationships

Before writing XPath expressions, always examine the HTML structure and determine whether parent-child or sibling relationships can simplify element identification.

Writing Overly Complex XPath Expressions

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


Best Practices

  • Use XPath Axes whenever element relationships improve locator reliability.

  • Prefer stable parent or sibling elements whenever possible.

  • Keep XPath expressions simple and readable.

  • Verify XPath expressions using browser Developer Tools.

  • Avoid unnecessarily complex navigation.

  • Write reusable XPath locators within automation frameworks.

  • Prefer simpler locator strategies whenever appropriate.


Conclusion

XPath Axes provide powerful navigation capabilities that allow Selenium to locate elements based upon their relationships with other HTML elements. They significantly improve locator flexibility when working with complex and dynamic webpages.

Understanding XPath Axes is an important step toward mastering advanced XPath techniques used extensively in professional Selenium automation frameworks.


Frequently Asked Questions (FAQs)

What are XPath Axes?

XPath Axes are navigation techniques used to locate web elements based upon their relationships with other elements in the HTML document.

Which XPath Axes are commonly used in Selenium?

The most commonly used XPath Axes include:

  • following-sibling

  • parent

  • child

  • ancestor

  • descendant

Why are XPath Axes useful?

They allow Selenium to navigate between related elements instead of searching the entire webpage.

When should I use XPath Axes?

Use XPath Axes whenever a target element can be reliably identified through its parent, child, sibling, or ancestor relationships.

Are XPath Axes useful for dynamic web elements?

Yes.

XPath Axes are frequently used when web elements contain dynamic attributes but maintain stable relationships with other HTML elements.


Key Takeaways

  • XPath Axes provide powerful navigation capabilities for locating related web elements.

  • following-sibling, parent, child, ancestor, and descendant are commonly used XPath Axes.

  • They improve locator flexibility and framework maintainability.

  • Prefer stable element relationships whenever possible.

  • Keep XPath expressions simple, readable, and reusable.

  • XPath Axes are widely used in professional Selenium automation frameworks.