Skip to main content

How XPath Works: The Basics You Should Know

Sophie avatar
Written by Sophie
Updated over a week ago

So, you’ve heard of XPath — the thing that helps you pull data from web pages. But how does it actually work? In this article, we’ll walk you through the basic concepts behind XPath so you can start using it with confidence.

Whether you're just curious or looking to troubleshoot your own web scraping workflows, this guide will help you understand what XPath is really doing under the hood.


What Is a Node in HTML?

Every webpage you visit is built from an HTML document — and every element on that page (a button, an image, a price tag) is part of a big tree structure. We call each of these elements a node.

Think of it like a family tree:

  • The entire page starts at the root node (usually <html>)

  • Inside that, there are child nodes (like <body>, <head>)

  • Those children have children of their own (like <div>, <p>, <img>, and so on)

Here's a simple example you might see on a shopping site:

<div class="product"> 
<h2 class="title">iPhone 15</h2>
<span class="price">$999</span>
</div>

In this snippet:

  • The outer <div> is the parent node

  • The <h2> and <span> are child nodes of that <div>

  • These child nodes are also siblings to each other

This hierarchy is what XPath helps you navigate — from the top of the tree all the way down to the specific piece of data you want.


How Are Elements Organized?

To understand XPath, it helps to think like a navigator. You’re starting at one point in the tree and trying to move to another.

  • A parent node contains other elements

  • A child is nested inside a parent

  • Siblings sit side-by-side under the same parent

  • Some nodes have attributes — extra bits of info (like class, href, or src) that you can also use in your XPath

Being aware of this structure helps you write better XPath rules that don’t just rely on “guesswork” but actually follow the document layout.


How Does XPath Locate a Node?

XPath is like giving directions through the HTML tree. You can tell it:

  • “Start from the very beginning (the root), then go down step by step” using /

  • Or “Find this type of node anywhere in the tree” using //

Let’s say you want to grab the product name in this snippet:

<div class="product">
<h2 class="title">iPhone 15</h2>
</div>

You could write:

//div[@class="product"]/h2[@class="title"]

This tells XPath:

  • Look for any <div> element with class “product”

  • Inside it, look for an <h2> with class “title”

The XPath is following the tree from the matching parent to its child.


Common HTML Elements You’ll See in XPath

There are a few tags that show up everywhere on web pages — and you’ll see them often in XPath expressions too:

  • <div> – A generic container (very common)

  • <span> – An inline container, often used for text

  • <a> – A link

  • <img> – An image

  • <h1> to <h6> – Headings

  • <p> – Paragraphs

  • <button> – You guessed it — buttons!

Each of these can be targeted by XPath based on tag name, class, or other attributes.


Basic XPath Syntax Rules

XPath may look a bit intimidating at first, but its basic rules are surprisingly simple:

  • / means “from the root” — start at the top of the tree and go down one level at a time Example: /html/body/div

  • // means “anywhere in the document” — more flexible but less precise Example: //div[@class="price"]

  • [@attribute="value"] filters nodes by their attributes Example: //a[@href="/login"]

  • Text inside an element can be matched with text() Example: //span[text()="Buy now"]

You can combine these rules to build powerful expressions — no need to memorize everything at once.


A Practical Example: Finding a Product Title on Amazon

Suppose you're scraping an Amazon product page and want to get the product title. You inspect the element and see something like this:

The XPath might be: //span[@id="productTitle"]

You’re telling XPath: “Find a <span> element that has an id of productTitle.”

Simple, right? Most of the time, you can write XPath this way — just by pointing to the tag and the attribute that makes it unique.


Using a Browser Extension

There are many free browser extensions like “XPath Helper” or “XPath Tester” that let you:

  • Hover over elements to see their XPath

  • Test XPath expressions in real time

  • Copy working XPaths without digging through the full HTML tree

These tools save tons of time and reduce guesswork when writing your XPath.

At its core, XPath is about understanding structure — not memorizing rules. Once you see how HTML is just a nested set of nodes, using XPath to pinpoint the data you want becomes a lot more intuitive.

In the next article, we’ll talk about absolute vs. relative XPath, and how choosing the right type of path can make your scraping tasks more reliable.

Did this answer your question?