XPath (XML Path Language) is a query language for navigating and selecting nodes in XML and HTML documents. In test automation, tools such as Selenium and Playwright use XPath expressions to locate elements on a web page so tests can click, type or read them.
Key components of XPath include:
- Absolute Paths: Expressions that start from the root of the document and follow every level. They break easily when the page structure changes, so they are best avoided in tests.
- Relative Paths: Expressions that start with // and search anywhere in the document, which makes them more resilient.
- Predicates: Conditions in square brackets filter matches by attribute, position or text, for example selecting a button whose text is Submit.
- Axes: Keywords such as parent, following-sibling and ancestor select elements based on their relationship to another element.
- Functions: Functions like contains() and text() match partial attribute values or visible text.
XPath is powerful but can produce brittle tests if expressions are tied to layout. Stable attributes such as test IDs, roles and labels usually make more reliable locators.


