Selenium WebDriver

Relative XPath Fundamentals

Master relative XPath techniques including single attributes, logical operators, text methods, and advanced XPath functions for robust element location.

Relative XPath Overview

Why Relative XPath?

Relative XPath is used to locate elements in web pages and is the most preferable XPath method because we can start searching elements from the middle of the HTML DOM structure.

🔑 Key Characteristics

  • • Starts with double forward slash (//)
  • • More flexible than absolute XPath
  • • Has different inbuilt methods and keywords
  • • Can start searching from any point in DOM
  • • Most robust and maintainable approach

Basic Syntax Structure

// Basic relative XPath syntax
//tagname[@attribute='value']

XPath Location Methods

1. Single Attribute XPath

Syntax

//tagname[@attribute='value']

Examples:

Facebook Registration Form
// First name input
//input[@name='firstname']
// Month dropdown
//select[@aria-label='Month']
//select[@name='birthday_month']
//select[@id='month']
Form Elements
//input[@id='userEmail']
//input[@placeholder='name@example.com']
//input[@value='Male']

2. XPath with AND Operator

Condition

Both attribute values should exist in the same element

Syntax

//tagname[@attribute='value' and @attribute='value']

Examples:

// Checkbox with multiple attributes
//input[@id='hobbies-checkbox-1' and @type='checkbox']
// Login button
//input[@data-test='login-button' and @id='login-button']
// Submit button
//input[@type='submit' and @class='submit-button btn_action']

3. XPath with OR Operator

Condition

Any one attribute value should exist in the same element

Syntax

//tagname[@attribute='value' or @attribute='value']

Examples:

//input[@name='login-button' or @value='Login']
//input[@id='u_0_8_p/' or @aria-label='First name']
//select[@id='dropdown-class-example' or @name='dropdown-class-example']

Text-Based XPath Methods

4. text() Method

Use Case

Locate elements using their physical text content (buttons, labels, links)

Syntax

//tagname[text()='value']

Examples:

// Button elements
//button[text()='Submit']
//div[text()='Login']
// Label elements
//label[text()='Female']
//label[text()='Music']
// Link elements
//a[text()='Forgot your password?']
//a[text()='Sign Up']

5. starts-with() Method

Use Case

Locate elements when starting value is constant and remaining value is dynamic

Syntax

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

Examples:

//a[starts-with(text(),'Forgot')]
//a[starts-with(text(),'Sign')]
//div[starts-with(text(),'Login')]
//label[starts-with(text(),'Female')]

6. contains() Method

Use Case

Locate elements using partial text values (any part of the text)

Syntax

//tagname[contains(text(), 'value')]

Examples:

//label[contains(text(),'Reading')]
//label[contains(text(),'Other')]
//a[contains(text(),'account')]

Comparison Example

// Same element, different approaches:
//a[text()='Already have an account?']
//a[starts-with(text(),'Already')]
//a[contains(text(),'account')]

7. normalize-space() Method

Use Case

Removes extra spaces (starting, middle, ending) and ignores case sensitivity

Syntax

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

Examples:

// Handles extra spaces
//button[normalize-space(text())='Login']
// Works with: ' Login ', 'Login', ' Login '
// Combined with other methods
//a[normalize-space(contains(text(),'google'))]
//a[normalize-space(starts-with(text(),'google'))]

Practical Implementation

Java Implementation Example

Facebook Registration Form

Locating month dropdown using different XPath approaches

// Multiple ways to locate the same element
WebElement monthDropdown1 = driver.findElement(By.xpath("//select[@name='birthday_month']"));
WebElement monthDropdown2 = driver.findElement(By.xpath("//select[@aria-label='Month']"));
WebElement monthDropdown3 = driver.findElement(By.xpath("//select[@id='month']"));

// Using AND operator for more specific targeting
WebElement loginBtn = driver.findElement(By.xpath("//input[@type='submit' and @class='submit-button btn_action']"));

// Using text() method for buttons
WebElement submitBtn = driver.findElement(By.xpath("//button[text()='Submit']"));

// Using normalize-space for buttons with extra spaces
WebElement loginBtnNorm = driver.findElement(By.xpath("//button[normalize-space(text())='Login']"));

Day 20 Knowledge Check

Question 1 of 5

What symbol does relative XPath start with?

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.