Skip to content
Home

/

Glossary

/

Browser

/

XPath

Browser

3 min read

What is 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 is a W3C language for navigating nodes in an XML or HTML document, complementing CSS selectors. Autonoly supports XPath targeting throughout its browser automation and data extraction.

    Why It Matters

    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.

    How Autonoly Solves It

    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.

    Learn more

    Examples

    • 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

    Frequently Asked Questions

    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.

    You might also like

    Blog Posts
    Use Cases

    Related terms, automations and guides

    Where this concept shows up in practice.

    DefinitionCSS SelectorA CSS selector is a pattern used to identify and target specific HTML elements on a web page, widely used in browser automation to locate buttons, forms, text, and other interactive elements.DefinitionDOMThe DOM (Document Object Model) is a tree-structured representation of a web page's HTML that browsers create in memory, allowing scripts and automation tools to read and manipulate page content programmatically.DefinitionBrowser AutomationBrowser automation is the use of software to control a web browser programmatically, performing tasks like clicking buttons, filling forms, and extracting data without manual human interaction.DefinitionWeb ScrapingWeb scraping is the automated process of extracting data from websites by programmatically reading and parsing HTML content. It enables businesses to collect structured information from web pages at scale without manual copy-and-paste.DefinitionData ExtractionData extraction is the process of retrieving structured or unstructured data from various sources — websites, documents, databases, APIs, or files — and converting it into a usable format for analysis, storage, or further processing.DefinitionPlaywrightPlaywright is Microsoft's open-source browser automation framework that provides a single API to control Chromium, Firefox, and WebKit browsers. It supports headless and headed modes, auto-waiting, and network interception, making it a leading choice for testing and web scraping.GuideWhy Your Scraper Returns an Empty List (and How to Fix It)Your selector works in Chrome and returns nothing in Python. Almost always the page you inspected is not the page you downloaded. A 30-second diagnosis, the six real causes, and the fix for BeautifulSoup, Scrapy, Playwright and lxml.GuideAutonoly vs Browse.ai: Web Scraping vs Full Browser AutomationAn honest comparison of Autonoly and Browse.ai. Browse.ai excels at simple web scraping and change monitoring. Autonoly handles the full spectrum — scraping, form filling, document processing, and multi-step workflows. We break down when each tool is the right choice.

    Stop reading about automation.

    Start automating.

    Describe what you need in plain English. Autonoly's AI agent builds and runs the automation for you — no code required.

    See Features