```yaml
product: AlterLab
title: "XPath — Web Scraping Glossary | AlterLab"
comparison_context: "AlterLab is an alternative to Firecrawl, ScrapingBee, and Bright Data."
source_url: https://alterlab.io/glossary/xpath
```

extraction

# XPath

XML Path Language — a query syntax for selecting nodes in HTML or XML using path expressions, more expressive than CSS selectors for complex traversals.

XPath (XML Path Language) is a query language for selecting nodes in XML and HTML documents using path expressions. While CSS selectors match elements by their styling attributes and structural relationships, XPath can express more complex traversals: selecting nodes based on their text content, selecting parent or sibling nodes from a known child, counting sibling elements, or applying arithmetic operations to attribute values.

XPath expressions follow a path syntax: `//` selects anywhere in the document, `/` selects direct children, `@` accesses attributes, `[]` applies predicates (conditions), and `text()` extracts text nodes. For example, `//div[@class='price']/span[1]/text()` selects the text of the first span inside any div with class "price".

XPath's greater expressiveness makes it valuable for situations where CSS selectors cannot express the required relationship — such as selecting a node based on adjacent sibling text or navigating up the DOM tree. It is the standard in XML processing ecosystems and is supported by lxml, Scrapy, and browser DevTools. AlterLab's structured extraction supports both CSS selector and XPath specification in requests.

### What is XPath?

XML Path Language — a query syntax for selecting nodes in HTML or XML using path expressions, more expressive than CSS selectors for complex traversals.

### How does AlterLab handle XPath?

XPath's greater expressiveness makes it valuable for situations where CSS selectors cannot express the required relationship — such as selecting a node based on adjacent sibling text or navigating up the DOM tree. It is the standard in XML processing ecosystems and is supported by lxml, Scrapy, and browser DevTools. AlterLab's structured extraction supports both CSS selector and XPath specification in requests.

## Examples

```
# XPath examples
//div[@class='price']/span/text()    # text inside span in .price div
//a[contains(@href, 'product')]      # links containing 'product' in href
//table//tr[position()>1]            # table rows after the header
//h2[following-sibling::p]           # h2 elements that have a sibling p
```

## Related Terms

[CSS SelectorA pattern used to match HTML elements by tag, class, ID, or attribute, used in web scraping to extract specific nodes from parsed HTML.](https://alterlab.io/glossary/css-selector)[Data ExtractionThe process of identifying and pulling specific fields from raw web content, converting unstructured HTML into typed JSON records.](https://alterlab.io/glossary/data-extraction)[BeautifulSoupA Python library for parsing HTML and XML and navigating the parse tree using CSS selectors or tag methods.](https://alterlab.io/glossary/beautifulsoup)[Structured Data ExtractionConverting free-form HTML into typed JSON records using explicit schemas, producing clean structured output instead of raw markup.](https://alterlab.io/glossary/structured-data-extraction)

## Extract XPath data from any website

AlterLab returns clean, structured data from any public URL — no scraper infrastructure needed. Start free, no credit card required.

[View API docs](https://alterlab.io/docs/api/rest)

[Back to Glossary](https://alterlab.io/glossary)