04 / Testing / xpath-tester
XPath Tester
Evaluate XPath expressions against XML or HTML.
How to use
- 01Type an XPath expression in the XPath expression field, or click an example chip such as //input[@type='email'] or count(//li).
- 02Choose HTML or XML in the Parse as dropdown to match your source document.
- 03Paste the page markup or XML document into the editor; a sample login page is preloaded.
- 04Review the Matches panel: node results list each node's name, trimmed text and outer HTML, while count(), string() and boolean expressions show a single value.
- 05Adjust the expression until the match count is exactly what your test expects, then copy it into your Selenium, Playwright or Cypress locator.
What is this tool?
An XPath tester that evaluates expressions with your browser's native XPath engine (document.evaluate) against HTML or XML you paste in. It handles node sets, attribute and text nodes, and scalar results from functions like count() and string(), and it reports invalid XPath and malformed XML with the engine's own error. Nothing leaves your browser.
XPath locator cheat sheet for test automation
// searches anywhere below the current node, while / steps to a direct child. Attribute tests go in brackets: //input[@name='email'] matches by exact value, //button[contains(@class,'primary')] matches a class token inside a longer class attribute, and //a[starts-with(@href,'/docs')] matches a prefix. Text matching uses //button[normalize-space()='Sign in'] or //li[contains(text(),'Beta')].
Positional predicates are 1-based: //li[1] is the first item and //li[last()] is the last. Wrap the path in parentheses, as in (//li)[2], to pick the second match in the whole document rather than the second child of each parent. Axes such as ancestor::form, following-sibling::li and parent::* let you navigate from a stable anchor to the element you actually need.
Worked example: against the preloaded sample, //ul[@class='items']/li returns three nodes, //li[last()]/text() returns the text node Gamma, //a/@href returns the two href attributes, and count(//li) returns 3 as a single number.
XPath vs CSS selectors for Selenium and Playwright
CSS selectors are shorter and are usually the first choice in Playwright and Cypress, but XPath can do two things CSS cannot: match on visible text and walk up the tree. //label[text()='Email']/following-sibling::input finds an input by its label, and //td[.='Order 42']/ancestor::tr selects the table row containing a cell, which is hard to express in plain CSS.
The trade-off is brittleness. Absolute paths such as /html/body/div[3]/form/input[2] break as soon as the layout changes. Prefer anchors that carry meaning, like data-testid, name or id attributes, and keep positional indexes as a last resort. Selenium accepts XPath via By.xpath, Playwright via the xpath= prefix or any selector starting with //, and Cypress through a plugin.
Why an XPath works in DevTools but fails in your test
Browsers support XPath 1.0 only, so XPath 2.0 functions like matches(), lower-case() or ends-with() will throw an Invalid XPath error here just as they would in Selenium. Use translate() for case folding and contains() or starts-with() instead.
The HTML parser also repairs markup: a table without a tbody gets one inserted, so //table/tr finds nothing while //table//tr works. If you paste real page source, also remember that content rendered later by JavaScript will not be present in the static HTML you test against. Switch Parse as to XML for SOAP responses, sitemaps or config files; malformed XML is reported instead of silently returning no matches.
Common use cases
- Building robust Selenium By.xpath locators before adding them to a page object
- Finding a form input by its label text with following-sibling axes
- Counting rows or list items to set an expected value in a test
- Querying SOAP or other XML API responses for a specific element value
- Checking why a locator copied from DevTools matches too many elements
FAQ
How do I test an XPath in the browser?
Paste the page HTML into the editor, type your expression in the XPath expression field and read the Matches panel. It uses the same document.evaluate engine that runs XPath in the browser.
Which XPath version is supported?
XPath 1.0, because that is what browsers implement. XPath 2.0 functions such as matches() or lower-case() are not available.
Can I test XPath against XML instead of HTML?
Yes. Set Parse as to XML. Invalid XML is reported with the parser's error message rather than returning zero matches.
Does it support count(), string() and other functions?
Yes. Expressions that return a number, string or boolean, such as count(//li) or string(//button), show a single value instead of a node list.
Is there a limit on the number of matches?
The first 500 matching nodes are listed, with text and HTML previews truncated for readability.
Is my HTML uploaded anywhere?
No. The document is parsed and queried locally in your browser.