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 에이전트가 자동화를 구축하고 실행합니다. 코딩 필요 없음.

    기능 보기