Skip to content
ホーム

/

用語集

/

ブラウザ

/

XPath

ブラウザ

2分で読了

XPathとは?

XPath (XML Path Language) is a query language for selecting nodes in XML and HTML documents, offering powerful navigation capabilities including parent traversal and conditional filtering.

What is XPath?

XPath (XML Path Language) is a query language for navigating and selecting nodes within XML and HTML documents. It uses a path-based syntax similar to file system paths to traverse the document tree and locate specific elements, attributes, or text content.

XPath Syntax Basics

XPath expressions describe a path through the document tree:

  • //div — Select all <div> elements anywhere in the document
  • //div[@class="product"] — Select divs with a specific class
  • //table/tbody/tr[position() > 1] — Select table rows, skipping the header
  • //a[contains(text(), "Next")] — Select links containing the text "Next"
  • //input[@type="email"]/parent::div — Select the parent div of an email input
  • XPath vs. CSS Selectors

    XPath offers capabilities that CSS selectors lack:

  • Upward traversal: XPath can select parent and ancestor elements (parent::, ancestor::)
  • Text content matching: XPath can filter elements by their text content (contains(text(), "..."))
  • Complex conditions: XPath supports boolean operators, numeric comparisons, and string functions
  • However, CSS selectors are generally preferred in browser automation for their simplicity and slightly better performance. XPath is most valuable when you need to locate elements relative to their text content or navigate upward in the DOM tree.

    XPath in Browser Automation

    Most automation frameworks including Playwright and Selenium support XPath alongside CSS selectors. XPath is particularly useful when elements lack unique IDs or class names but have distinctive text content, or when you need to find an element based on its relationship to a sibling or parent element.

    なぜ重要か

    XPath provides an alternative to CSS selectors that can handle complex element selection scenarios, particularly when elements need to be located by their text content or relative position to other elements in the DOM tree.

    Autonolyのソリューション

    Autonoly's AI agent can use both CSS selectors and XPath expressions to locate elements, choosing the most appropriate method based on the page structure. When CSS selectors prove too fragile, the agent can fall back to text-based XPath queries for more resilient element targeting.

    詳しく見る

    • Using `//button[contains(text(), 'Submit')]` to find a submit button by its visible text

    • Selecting `//td[text()='Total']/following-sibling::td` to extract the value next to a 'Total' label

    • Targeting `//div[@data-testid='results']//a` to find all links within a results container

    よくある質問

    CSS selectors are generally preferred for their simplicity and performance. Use XPath when you need capabilities CSS lacks — selecting elements by text content, navigating to parent elements, or writing complex conditional queries. Many experienced automation engineers use CSS selectors as their default and switch to XPath only when CSS cannot express the needed selection.

    Right-click the element in Chrome DevTools' Elements panel, select Copy, then choose 'Copy XPath' or 'Copy full XPath'. The copied XPath will work for automation but may be overly specific. Consider simplifying it by using attributes or text content rather than positional paths.

    自動化について読むのはここまで。

    自動化を始めましょう。

    必要なことを日本語で説明するだけ。AutonolyのAIエージェントが自動化を構築・実行します。コード不要。

    機能を見る