XPath selectors
XPath (XML Path Language) selectors provide a powerful alternative to CSS selectors for targeting elements in Doc Detective tests. While more complex than CSS selectors, XPath offers advanced capabilities for navigating complex HTML structures and selecting elements based on their content or relationships.
When to use XPath selectors
Use XPath selectors in Doc Detective actions like find, click, and others that accept a selector property. XPath is useful when:
- You need to select elements based on their text content
- You need to navigate up the DOM tree (select parent elements)
- CSS selectors aren’t powerful enough for your use case
- You need to select elements by their position relative to other elements
For most use cases, CSS selectors are simpler and more readable. Use XPath only when CSS selectors can’t accomplish what you need.
XPath syntax basics
On browser surfaces, Doc Detective treats a selector starting with //, /, or ( as XPath (otherwise it treats the selector as CSS). The ( case covers grouped expressions like (//button)[2], used to index or filter a node set. See Position-based selection.
On native app surfaces, Doc Detective recognizes only // and ( as XPath. A bare single / doesn’t count. Stick to // (or a (...) grouped expression) if the same selector needs to work on both browser and app surfaces.
Absolute vs. relative paths
-
Absolute path: Starts with a single
/and specifies the complete path from the rootAvoid absolute pathsThey’re fragile and break when the page structure changes.
-
Relative path: Starts with
//and finds elements anywhere in the documentPrefer relative pathsThey’re more flexible and maintainable.
Basic XPath patterns
Select by element type
Find all elements of a specific type anywhere in the document:
Select by attribute
Find elements with a specific attribute value using the [@attribute='value'] syntax:
Select by ID
Target elements by their id attribute using the @id selector:
The * matches any element type. You can also be more specific: //input[@id='username'].
Select by class
Use the contains() function to match elements by their class attribute:
Use contains() because an element can have multiple classes. This matches any element whose class attribute contains “submit-button”.
Select by text content
One of XPath’s most powerful features is selecting elements by their visible text:
This finds a button element with the exact text “Submit”.
Select by partial text
Use the contains() function to match elements that contain specific text:
This finds a button element whose text contains “Submit” (like “Submit Form” or “Click to Submit”).
Combining conditions
Multiple attributes
Use the and operator to combine multiple attribute conditions:
OR conditions
Use the or operator to match elements that satisfy any of the specified conditions:
Element type and attribute
Combine element types with attribute matching and text content for highly specific selectors:
Navigating the DOM
Parent elements
Select the parent of an element using the parent:: axis or the .. shorthand:
Or use the shorter form:
Ancestor elements
Find any ancestor element (not just the direct parent) using the ancestor:: axis:
Following siblings
Select sibling elements that come after the current element using the following-sibling:: axis:
This finds an input that comes after a label with text “Email”.
Preceding siblings
Select sibling elements that come before the current element using the preceding-sibling:: axis:
Descendant elements
Find elements nested at any depth within other elements using the // operator:
This finds any input within a div with class “form-group”, regardless of nesting depth.
Position-based selection
Select by index
Select a specific occurrence using square brackets with a number (XPath indexes start at 1):
This selects the second button element in the document.
Use parentheses to select from all matching elements. Without them, //button[2] means “a button that is the second button child of its parent.”
First element
Select the first matching element using index [1]:
Last element
Select the last matching element using the last() function:
Position greater than
Select elements beyond a certain position using the position() function:
This selects all li elements from the third onwards.
Advanced functions
Attribute contains
Match elements whose attributes contain a specific substring:
Attribute starts with
Match elements whose attributes begin with a specific value:
Case-insensitive text matching
XPath 2.0+ supports case-insensitive matching, but browser support varies. For compatibility, use:
This converts text to lowercase before matching.
Not operator
Exclude elements with specific attributes using the not() function:
This finds enabled buttons (those without a disabled attribute).
Best practices
Prefer CSS selectors when possible
XPath is powerful but complex. Use CSS selectors for more straightforward cases:
#username or [data-testid='login-button']
//*[@id='username'] or //*[@data-testid='login-button']
Use relative paths
//input[@type='password']
/html/body/div[1]/form/div[2]/input[3]
Combine text and attributes for specificity
Create highly specific selectors by combining text content with attributes:
Test selectors in browser console
Use the $x() function in the browser console to test XPath selectors:
This returns an array of matching elements.
Avoid brittle position-based selectors
//div[3]/span[2]/a[1]
//a[contains(@class, 'download-link')]
Common patterns
Find input by label text
Target an input field based on its associated label text:
Or if the input is nested inside the label:
Find element by placeholder
Target input fields by their placeholder text:
Find button by aria-label
Target buttons and other elements by their ARIA accessibility labels:
Find link by partial href
Target links that contain specific URL patterns:
Examples
Click a button by text
Find input by following a label
Find enabled submit button
Select third item in a list
Find element with specific data attribute
Debugging XPath selectors
Test in browser console
Use the $x() function (available in most browser consoles) to test and debug your XPath selectors:
Browser developer tools
- Open Developer Tools (F12)
- Go to the Console tab
- Type
$x("your-xpath-here")and press Enter - The console shows all matching elements
- Hover over results to highlight them on the page
Common issues
- Verify the element exists in the DOM
- Check for typos in attribute names or values
- Ensure the element is loaded (you may need to add a
waitaction)
- Add more specific conditions
- Combine multiple attributes
- Use text content to narrow the match
- Doc Detective detects the selector type automatically from the leading characters.
- On browser surfaces, start your XPath with
//,/, or(. Doc Detective treats anything else as CSS. - On native app surfaces, only
//and(count as XPath. A bare single/is treated as CSS.
Related resources
- CSS selectors: Simpler selector syntax (recommended for most cases)
findaction: Locate and interact with elementsclickaction: Click elements using selectors- MDN XPath Documentation: Comprehensive XPath reference
- XPath Cheat Sheet: Quick reference for XPath syntax