Selenium WebDriver

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

//tagname[starts-with(@attribute, 'value')]

Examples:

// Dynamic ID with constant prefix
//input[starts-with(@id, 'u_0_h')]
// Dynamic name attribute
//input[starts-with(@name, 'reg_email__')]
// ID starting with 'first'
//input[starts-with(@id,'first')]

💡 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

//tagname[contains(@attribute, 'value')]

Examples:

// ID containing specific pattern
//input[contains(@id, 'u_0_h')]
// Name containing 'email__'
//input[contains(@name,'email__')]
// ID containing 'first'
//input[contains(@id,'first')]

🔍 Comparison

starts-with: "abc123def" matches starts-with(@attr, 'abc')
contains: "abc123def" matches contains(@attr, '123')

10. normalize-space() with Attribute

Use Case

When attribute values have extra spaces that need to be normalized

Syntax

//tagname[normalize-space(@attribute)='value']

⚠️ 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

//tagname[@attribute='value']/child::tagname
//tagname[@attribute='value']/child::tagname[@attribute='value']
//tagname[@attribute='value']/child::tagname[position()=1]

Examples:

// Basic child navigation
//div[@class='b11']/child::input
// First child element
//div[@class='b11']/child::input[position()=1]
// Complex nested navigation
//div[contains(@id,'u_0_7')]/child::div[position()=1]/child::input

🏗️ DOM Structure Example

<!-- Grant Parent (div) -->
  <!-- Parent 1 div --> class="uiStickyPlaceholderInput"
    <!-- child div -->
    <!-- child input -->
  <!-- Parent 2 div --> class="_1pc_"

2. descendant Keyword

Purpose

Search all child tags, grandchild tags, and great-grandchild tags (entire subtree)

Syntax Variations

//tagname[@attribute='value']/descendant::tagname
//tagname[@attribute='value']/descendant::tagname[@attribute='value']
//tagname[@attribute='value']/descendant::tagname[position()=1]

Examples:

// Find any input descendant
//div[contains(@id, 'u_0_7')]/descendant::input
// Complex form navigation
//span[@class='b1']/descendant::input[@name='firstname'][position()=1]
// Google search button
//div[@class='FPdoLc lJ9FBc']/descendant::input[@name='btnK']
// Amazon price element
//span[@data-csa-c-item-id='amzn1.asin.B0DGHYDZR9']/descendant::span[@class='a-price-whole']

🔍 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

// Navigate to nested elements using combined axes
//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

Day 21 Knowledge Check

Question 1 of 5

What is the difference between starts-with() with text() and starts-with() with attribute?

SDET Mastery

Master Test Automation

Home
CurriculumPracticeQ&ACheatsheet
🍵Buy me a Chai

Automation Testing Course

Comprehensive course covering Manual Testing, Java Programming, and Selenium WebDriver

🍵Buy me a Chai
Privacy Policy•GitHub
© 2024 Automation Testing Course. All rights reserved.