XPath Tester

Test XPath expressions against XML and HTML with live results

Document type:

Ctrl+Enter to run

Quick examples:

Enter an expression and press Run

Tips

  • // searches anywhere Β· / selects direct children
  • @attr for attributes Β· text() for text nodes
  • [1] first Β· [last()] last Β· [position()<3] range
  • contains(@class,'x') for partial class matching

About this tool

The ToolNinja XPath Tester is a free online XPath evaluator and debugger. Paste any XML or HTML document, enter an XPath expression, and instantly see all matching nodes highlighted with their values, types, and positions β€” directly in your browser. XPath (XML Path Language) is the standard query language for navigating XML and HTML documents. Use this XPath tester online to select elements by tag name, filter by attribute values, traverse parent-child relationships with axis expressions (child::, parent::, ancestor::, descendant::), or extract text content and attribute values. The tool supports all XPath 1.0 expressions: location paths, predicates, wildcards, node tests, and built-in functions (string(), number(), count(), contains(), starts-with(), and more). Switch between XML and HTML mode β€” HTML mode uses the browser's DOMParser in HTML mode so you can test XPath against real web content. Perfect for web scraping, XML data extraction, XSLT development, API XML parsing, and learning XPath syntax. Everything runs 100% in your browser using the native document.evaluate() API β€” your data never leaves your machine.

When to use it

  • β†’Testing XPath selectors for web scraping scripts before running them
  • β†’Debugging XPath expressions in XSLT stylesheets or XML transformation pipelines
  • β†’Extracting data from XML API responses or SOAP envelopes
  • β†’Learning XPath syntax with instant visual feedback on a real document
  • β†’Validating XPath queries for Selenium, Playwright, or Robot Framework tests
  • β†’Inspecting HTML structure to find reliable XPath selectors for browser automation

Tips

  • β—†Use // to search anywhere in the document: //div[@class='price'] finds all divs with that class regardless of nesting.
  • β—†Predicates with position() or [n] select specific nodes: //li[1] selects the first list item, //li[last()] selects the last.
  • β—†The text() node test selects text content: //h1/text() returns just the text inside h1 tags, not the element itself.
  • β—†Use @attribute to select by attribute: //*[@id='main'] selects any element with id='main'. @* selects all attributes.
  • β—†contains() handles partial matches: //a[contains(@href, 'github')] finds links where the href includes 'github'.

Frequently asked questions

What is XPath and when should I use it?

XPath (XML Path Language) is a query language for selecting nodes from XML and HTML documents. Use it when you need to extract specific elements from XML APIs or feeds, write web scraping selectors, define locators for browser automation (Selenium, Playwright), or build XSLT transformations. CSS selectors are often simpler for HTML, but XPath is more powerful β€” it can navigate upward in the tree (parent/ancestor axes), select by text content, and handle XML namespaces.

What is the difference between / and // in XPath?

A single / is a direct child step β€” /root/child means 'child is a direct child of root'. Double // is the descendant-or-self axis shorthand β€” //child means 'child anywhere in the document'. Use // when you don't know or don't care how deeply nested the target element is. Be careful with // on large documents as it scans every node.

Why does my XPath expression return no results on HTML?

HTML documents may have a default namespace or case differences that trip up XPath. Ensure you're using HTML mode in the tester. Common issues: HTML tag names are uppercase in some parsers (use local-name() or uppercase), elements may be nested differently than expected, or the document uses namespaces that need to be declared. Try a simpler expression first (//*) to verify the document parsed correctly.

What is the difference between XPath and CSS selectors?

CSS selectors can only traverse downward (parent to child/descendant). XPath can navigate in any direction β€” up to parents and ancestors, sideways to siblings, and down to children and descendants. XPath can also select by text content (text()), by attribute value with functions like contains(), and works on both XML and HTML. Use CSS selectors for simple HTML selection; use XPath when you need to navigate upward, match text content, or work with XML.

Related tools

πŸ₯· ToolNinja