黑色星期五
& 聖誕節優惠
進階方案
額外加贈 60%
50GB + 免費 30GB
獲得額外流量
靜態住宅代理最高可享
7 折
50-99 個 IP 享 7 折
選購優惠 IP
無限流量
75 折
帶寬與端口套餐
立即使用無限流量
錢包儲值
最高 $1,400
獎勵金
立即儲值

XPath

XPath

XPath (XML Path Language) is a query language used to locate and select elements, attributes, and values within XML or HTML documents.

It describes paths through a document's hierarchical structure, making it useful for web scraping, browser automation, XML processing, and automated testing.

How Does XPath Work?

XPath treats a document as a tree of nodes.

A typical process includes:

  1. Parse an HTML or XML document.
  2. Identify the structure of the target element.
  3. Create an XPath expression.
  4. Evaluate the expression against the document.
  5. Return matching nodes, attributes, or text values.

XPath can select elements by tag name, attribute, position, text, or relationship to other elements.

Common XPath Syntax

Expression Meaning
/ Selects from the root
// Selects matching descendants
@ Selects an attribute
* Matches any element
[ ] Applies a condition
text() Selects text content

For example:

//h1

selects all h1 elements.

This expression:

//div[@class='product']

selects div elements whose class attribute equals product.

Absolute vs. Relative XPath

XPath expressions can use absolute or relative paths.

  • Absolute XPath: Starts from the document root and describes the complete path.
  • Relative XPath: Starts from a matching point anywhere in the document.

Example:

/html/body/div/h1

is an absolute XPath.

//h1

is a relative XPath.

Relative XPath is often easier to maintain when webpage layouts change.

XPath vs. CSS Selectors

XPath and CSS selectors can both locate elements in HTML.

  • XPath: Supports navigation through parent, child, sibling, and descendant relationships.
  • CSS Selectors: Use CSS-style syntax and are often simpler for common HTML selections.

CSS selectors are convenient for straightforward element selection, while XPath can be more powerful for complex document relationships.

Advantages of XPath

  • Flexible selection: Finds elements using attributes, text, position, and relationships.
  • Hierarchical navigation: Moves through parent, child, and sibling nodes.
  • Broad tool support: Works with XML parsers, Selenium, Scrapy, and other automation tools.
  • Precise extraction: Can target specific elements within complex documents.

Limitations of XPath

  • Complex syntax: Advanced expressions can become difficult to read.
  • Fragile absolute paths: Small layout changes can break long XPath expressions.
  • Dynamic content: JavaScript-generated elements may not exist until a page is rendered.
  • Performance: Very broad or complicated expressions can be slower on large documents.

XPath and Web Scraping

XPath is widely used in web scraping to extract structured information from HTML.

For example, a scraper may use XPath to locate:

  • Product names
  • Prices
  • Links
  • Images
  • Ratings
  • Table values

Frameworks such as Scrapy and browser automation tools such as Selenium support XPath selectors directly.

Example

Suppose a webpage contains:

<div class="product">
  <h2>Wireless Mouse</h2>
  <span class="price">$29.99</span>
</div>

XPath can select the product name with:

//div[@class='product']/h2/text()

The price can be selected with:

//div[@class='product']/span[@class='price']/text()

The extracted values are:

Wireless Mouse
$29.99

This allows a scraper to convert webpage content into structured product data.

Key Takeaway

XPath is a query language for locating and extracting information from XML and HTML documents.

Its ability to navigate document hierarchies and filter elements precisely makes it valuable for web scraping, automated testing, and structured data processing.

Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email