Member-only story
XPath (XML Path Language) is a powerful tool for navigating and selecting elements within an XML or HTML document. It is widely used in test automation, especially for web testing with tools like Selenium, Cypress, Playwright, and Robot Framework. Understanding XPath is crucial for creating robust, reliable locators that interact with elements effectively. 🤖
This guide will take you from the basics of XPath to advanced techniques, covering everything you need to create, understand, and master XPath expressions.

1. What is XPath? 📖
XPath is a query language used to select nodes from an XML document. In web testing, XPath is used to locate elements within an HTML document (which is a type of XML). XPath allows you to traverse and locate elements by defining paths, relationships, and attributes, making it a versatile tool for web automation.
Key Concepts 🔑
- Node: Basic building blocks of an XML/HTML document (e.g., elements, attributes, text).
- Path: A way to traverse and locate nodes within the document.
2. Basic XPath Syntax 🔤
XPath syntax resembles a file path, making it intuitive for navigating documents. The basic syntax is straightforward:
//tagname[@attribute='value']
Examples:
//div
selects all<div>
elements.//input[@id='username']
selects the<input>
element with theid
ofusername
.//a[text()='Login']
selects the<a>
element with the exact textLogin
.
3. Types of XPath 🧩
3.1 Absolute XPath 🛤️
Absolute XPath starts from the root node (/
) and traverses down to the desired element. It is precise but brittle, as any change in the structure can break the locator.
Example:
/html/body/div[1]/div[2]/form/input
Pros 👍:
- Direct and precise.
Cons 👎:
- Highly dependent on the structure; minor changes can break the XPath.