nodename
Browse / XPath Selectors Quick Guide
XPath Selectors Quick Guide
Unlock the power of web scraping and automation with this essential XPath selectors cheatsheet. Covering fundamental syntax, axes, predicates, functions, and common techniques for precise element location in XML and HTML documents.
Basic XPath Selectors
Selecting Nodes
|
Selects all nodes with the specified name. |
|
Selects the root node. (Absolute path) |
|
Selects nodes in the document from the current node that match the selection no matter where they are. (Relative path) |
|
Selects the current node. |
|
Selects the parent of the current node. |
|
Selects the specified attribute. |
|
Selects any element node. |
|
Selects any attribute of the current node. |
|
Selects any node of any kind (element, attribute, text, comment, processing instruction). |
Predicates (Conditions)
|
Filters a node-set based on the condition(s) inside the brackets. |
|
Selects the first node in the node-set. |
|
Selects the last node in the node-set. |
|
Selects nodes with position less than 3 (i.e., the first two nodes). |
|
Selects nodes where the specified attribute equals a specific value. |
|
Selects nodes that have the specified attribute, regardless of its value. |
|
Selects nodes whose entire text content equals ‘text’. |
|
Combines conditions using |
|
Combines conditions using |
Combining Paths
|
Selects nodes matching |
|
Selects nodes matching |
|
Selects nodes matching either |
|
Selects the first node from the combined set of nodes matching |
|
Selects all |
|
Selects the first |
XPath Axes & Functions
XPath Axes
|
General syntax for selecting nodes relative to the current node. |
|
Selects the current node itself. |
|
Selects children of the current node. (Default axis, same as |
|
Selects the parent of the current node. (Same as |
|
Selects descendants (children, grandchildren, etc.) of the current node. (Same as |
|
Selects all ancestors (parent, grandparent, etc.) of the current node. |
|
Selects all siblings after the current node, at the same level. |
|
Selects all siblings before the current node, at the same level. |
|
Selects all nodes that appear after the current node in the document, excluding descendants. |
|
Selects all nodes that appear before the current node in the document, excluding ancestors. |
Common XPath Functions
|
Selects the text node(s) of the current node. |
|
Checks if the first string contains the second string. |
|
Checks if the first string starts with the second string. |
|
(XPath 2.0) Checks if the first string ends with the second string. |
|
Converts the object to its string value. |
|
Removes leading/trailing whitespace and replaces internal sequences of whitespace with a single space. |
|
Returns the number of nodes in a node-set. |
|
Returns the position of the current node within the node-set being processed. |
|
Returns the index of the last node in the node-set being processed. |
Operators
|
Comparison operators. |
|
Arithmetic operators. |
|
Logical operators (used in predicates). |
|
Union operator (combines node sets). |
Advanced & Practical XPath
Selecting by Text Content
|
Selects |
|
Selects |
|
Selects |
|
Selects |
|
Selects |
|
Selects |
|
Selects |
Tips for Robust XPath
Avoid relying solely on position: |
Prefer using attributes: If an element has a unique ID ( |
Use text content where appropriate: For static text elements like labels, use |
Combine attributes and text: |
Utilize axes for relationships: If an element lacks unique identifiers but a related element (like a label) does, use axes: |
Test thoroughly: Always test your XPath in a browser’s developer console or dedicated tool to ensure it selects exactly what you intend and nothing else. |
Handle dynamic content: For elements loaded dynamically, you might need to wait for them to appear before attempting to locate them with XPath. |
CSS Selectors vs. XPath (Quick Reference)
CSS Selector |
Equivalent XPath |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|