Advanced XPath & Axes
Master advanced XPath techniques including attribute-based methods, XPath axes like child and descendant, and complex DOM navigation strategies.
Complete XPath Methods Overview
10 XPath Location Techniques
Text-Based Methods (1-7)
- 1. Single attribute
- 2. AND Operator
- 3. OR Operator
- 4. text() method
- 5. starts-with() with text()
- 6. contains() with text()
- 7. normalize-space() with text()
Attribute-Based Methods (8-10)
- 8. starts-with() with attribute
- 9. contains() with attribute
- 10. normalize-space() with attribute
Attribute-Based XPath Methods
8. starts-with() with Attribute
Use Case
When attribute value is dynamic at the end, but starting value is constant
Syntax
Examples:
💡 Real-world Scenario
Facebook generates dynamic IDs like "u_0_h_abc123", "u_0_h_def456". Using starts-with(@id, 'u_0_h') captures all variations.
9. contains() with Attribute
Use Case
When some part of attribute value is constant and remaining is dynamic
Syntax
Examples:
🔍 Comparison
10. normalize-space() with Attribute
Use Case
When attribute values have extra spaces that need to be normalized
Syntax
⚠️ Note
Less commonly used with attributes compared to text(), but useful for handling inconsistent attribute formatting.
XPath Axes Navigation
1. child Keyword
Purpose
Navigate to direct child elements of a parent element
Syntax Variations
Examples:
🏗️ DOM Structure Example
2. descendant Keyword
Purpose
Search all child tags, grandchild tags, and great-grandchild tags (entire subtree)
Syntax Variations
Examples:
🔍 child vs descendant
child::
Only direct children (one level down)
descendant::
All levels down (children, grandchildren, etc.)
Combined Axes Example
Complex Navigation
Combining descendant and child axes for precise element targeting
//div[@id='22d485af-325e-4680-a97f-289359347ee4']
/descendant::h2
/child::span
💡 Best Practices
- • Use child:: when you know the exact parent-child relationship
- • Use descendant:: when the element could be at any level below
- • Combine with position() for specific element selection
- • Use contains() and starts-with() with dynamic attributes