XPath
XPath is a query language for navigating and selecting nodes in an XML or HTML document, used in test automation to locate elements by their position, attributes, or text content — including relationships CSS selectors can't easily express, like "find the parent of this element" or "find text matching X."
XPath's key advantage over CSS selectors is expressiveness: it can traverse upward (to a parent or ancestor), select based on visible text content, and combine conditions in ways CSS selectors structurally can't — useful when there's no clean class or ID to target and the only reliable anchor is the element's text or its relationship to another element.
That same expressiveness is also its downside: complex XPath expressions tend to be harder to read, slower to evaluate, and more brittle against markup changes than an equivalent CSS selector — most modern locator-strategy guidance recommends CSS selectors or test IDs by default, reaching for XPath only when CSS genuinely can't express what's needed.
Example
//button[text()="Submit"]
//div[@class="cart-item"][1]//button[@aria-label="Remove"]
//label[text()="Email"]/following-sibling::inputXPath expressions using text content and sibling relationships CSS can't easily match.