# XPath

> XPath (XML Path Language) is a standard query language for navigating and selecting elements in XML and HTML documents. It lets you pinpoint specific nodes — like all product prices on a page or the canonical link in the head — using path expressions.

Search engine optimisation · Updated 2026-08-29 · [Marketing glossary](https://www.getlooploop.com/glossary)

**In short** XPath is a way to find and pick out specific pieces of a web page's code, like grabbing every headline or checking if a meta tag exists.

## What it is and how it works

XPath models an XML or HTML document as a tree of nodes: elements, attributes, text, comments, and more. A location path consists of steps separated by `/` or `//`. Each step has an axis (default `child`), a node test (like `div` or `@href`), and optional predicates in square brackets that filter the node set (e.g., `[@class='price']`). The engine evaluates the path from a context node — often the document root — and returns a node set in document order. Axes like `ancestor`, `following-sibling`, or `descendant` let you traverse sideways or upward, not just down. Predicates can use functions such as `contains()`, `starts-with()`, or `position()` to refine matches. Because XPath 1.0 is implemented in every browser and most scraping libraries, it works the same in Chrome DevTools, Python lxml, Screaming Frog, and custom crawlers.

## What to do about it

Start by learning the 20% of syntax you'll use daily: `//` for any descendant, `@attr` for attributes, `text()` for inner text, and predicates for filtering. Open Chrome DevTools, press Ctrl+F in the Elements panel, and type an XPath like `//meta[@property='og:title']` to verify it selects the right tag. Build a cheat sheet of your site's recurring patterns — product cards, pagination links, breadcrumb items — and turn them into reusable XPath expressions. Add those expressions to your crawler configuration (Screaming Frog custom extraction, Sitebulb, or a Python script) so every audit automatically pulls the same data points. Schedule a weekly spot-check: run the XPath against a handful of live URLs and confirm the extracted values still match what you see in the browser.

## How it is measured or noticed

You'll see XPath in three places. First, in SEO tool settings: custom extraction fields, JavaScript rendering rules, or structured data validators often accept XPath. Second, in code repositories: search for `xpath`, `lxml`, `selenium`, or `//` in your team's scrapers and test suites. Third, in browser consoles: typing `$x("//h1")` returns matching nodes instantly. If a crawler reports missing data, check whether the XPath still matches the live DOM — site redesigns often change class names or wrapper elements, breaking previously stable paths. A sudden drop in extracted fields across many URLs is a strong signal that an XPath needs updating.

## Common mistakes

- Using absolute paths like `/html/body/div[2]/section/div` — they break when any wrapper is added or removed.
- Relying on auto-generated class names such as `css-1x2y3z` — these change on every deploy.
- Ignoring namespaces on XML sitemaps or Atom feeds — `//url` returns nothing if the default namespace isn't registered.
- Assuming XPath sees JavaScript-rendered content — it only sees the initial HTML unless you run it inside a headless browser.
- Writing overly broad expressions like `//div` that return thousands of nodes and slow down crawls.

## Limits

XPath operates on the parsed DOM tree, not on visual layout. It cannot select elements based on CSS computed styles, screen position, or user interactions. It also does not understand JSON-LD embedded in `` tags — you must extract the script text first, then parse the JSON separately. XPath 1.0 lacks regular expressions and advanced string manipulation; for those you need XPath 2.0/3.0 (Saxon, BaseX) or post-processing in your host language. It is often confused with CSS selectors, which are shorter for simple cases but less powerful for sibling/ancestor traversal and text-based filtering. Finally, XPath cannot query across iframes or shadow DOM boundaries without switching context.

## Worked example

This expression finds every product card by a stable partial class match, then drills down to the price span that carries a Schema.org `itemprop`, and finally returns the `content` attribute value — the machine-readable price. It survives a redesign that adds wrapper divs or changes the exact class suffix, because `contains(@class,'product-card')` still matches. Test it in DevTools with `$x("//div[contains(@class,'product-card')]//span[@itemprop='price']/@content")` and you'll get an array of attribute nodes whose `value` properties are the prices you need.

> //div[contains(@class,'product-card')]//span[@itemprop='price']/@content

## Quick console test

In Chrome, open DevTools → Console and type `$x("your-xpath-here")`. It returns an array of DOM nodes you can inspect instantly — no extension needed.

## Frequently asked questions

### What is XPath and why do SEO tools use it?

XPath is a query language for selecting nodes in an XML or HTML document tree. Crawlers, scrapers, and audit tools use it because it gives a precise, repeatable way to target elements like the canonical link, H1, or product price without depending on visual position on the page.

### How is XPath different from CSS selectors?

Both select DOM nodes, but XPath can move in any direction through the tree, filter by text content, and target nodes by position. CSS selectors only travel downward and match by tag, class, or attribute, so tasks like "find the link whose anchor text contains 'Privacy'" usually need XPath.

### Do I need to learn XPath for technical SEO?

It depends on how deep you go. If you only run audits, most tools let you point and click. If you build custom extractions in Screaming Frog, custom scrapers, or Looker Studio connectors, basic XPath pays for itself within an afternoon.

### Why does my XPath match nothing when the page clearly has the element?

The most common reason is namespace handling, especially with feeds or SVG. Another frequent cause is matching against rendered text when the element is hidden, or using `text()` when you need `string(.)` to concatenate child nodes.

### Can XPath see elements that are rendered with JavaScript?

Only after they exist in the DOM. XPath evaluates the parsed HTML, so if your tool fetches the raw source before JS executes, dynamically injected nodes will be missing and your expression will return no results.

## Asked out loud

The same term in the words somebody uses speaking to an assistant rather than typing into a box — written from the situation, which is why each one carries the situation it came from.

### I've got a Screaming Frog custom extraction open and I need to pull every price on this category page before the client call in ten minutes — what syntax actually works?

Use `//span[@itemprop='price']/@content` to grab the Schema.org price attribute directly, or `//*[contains(@class,'price')]` if the markup is inconsistent. Paste the expression into the extraction field, run a test, and confirm the preview shows the values you expect before the meeting.

### My scraper is returning empty results for a page I can see in the browser — am I picking the wrong selector?

Yes, almost certainly. Pages rendered with JavaScript often have nodes that do not exist in the raw HTML your scraper downloaded. Switch your fetcher to a headless browser, or target the underlying JSON the page hydrates from, and the same XPath will start returning values.

### I keep writing selectors based on classes the developer keeps renaming — is there a stable way to grab the title on every product page?

Anchor on the schema, not the styling. Something like `//h1` or `//*[@itemprop='name']` survives redesigns because the meaning does not change, while a class like `.product-title__heading--xl` rarely makes it through a redesign. Test against a handful of pages from different templates before you trust it.

## Sources

- [W3C XPath 1.0 Specification](https://www.w3.org/TR/xpath/)

Source: GetLoopLoop Glossary — https://www.getlooploop.com/glossary/xpath. CC BY 4.0: https://creativecommons.org/licenses/by/4.0/

---

Canonical page: https://www.getlooploop.com/glossary/xpath
This is the Markdown copy of that page for AI agents, made from the same content.

Other languages: [uk](https://www.getlooploop.com/uk/glossary/xpath.md) · [es](https://www.getlooploop.com/es/glossary/xpath.md)
