
π Master XPath in Selenium
XPath Tutorial in Selenium
Master XPath in Selenium: XPath is one of the most powerful locator strategies in Selenium WebDriver, especially when elements donβt have consistent IDs or classes. In this guide, weβll explore advanced XPath functions like:
- contains() β for partial matching
- following-sibling β to locate siblings of elements
- ancestor β to trace elements back to their parent hierarchy
- Logical operations (AND, OR)
- parent, starts-with, and XPath axes like following, preceding, and descendant
Table of Contents
- What is XPath contains() in Selenium?
- XPath following-sibling in Selenium
- XPath ancestor in Selenium
- Using AND & OR in XPath
- XPath parent function
- XPath starts-with()
- XPath axes: following, preceding, descendant
- Summary
π What is XPath contains() in Selenium?
Master XPath in Selenium: The contains() function is used to locate web elements that partially match a string value in their text or attribute.
β Syntax:
xpath
//*[contains(text(), ‘SAP M’)]
This XPath finds any element whose text contains the substring “SAP M”.
β Use Case:
When web element text is dynamic or only partially known, contains() helps in matching without needing the full value.
π 2. XPath following-sibling in Selenium
Master XPath in Selenium: following-sibling helps locate a sibling element that follows a known element in the DOM hierarchy.
β Syntax:
xpath
//a[text()=’SELENIUM’]/following-sibling::h4
This finds the <h4> element that follows an <a> tag with text “SELENIUM”.
π 3. XPath ancestor in Selenium
Master XPath in Selenium: The ancestor function in XPath is used to navigate upwards in the DOM and locate parent or grandparent elements.
β Syntax:
xpath
//a[text()=’SELENIUM’]/ancestor::div
This will find the div container that is an ancestor of the anchor element with text “SELENIUM”.
π 4. Using AND and OR in XPath
β AND:
Both conditions must be true to locate the element.
xpath
//input[@type=’text’ and @name=’username’]
β OR:
Any one condition being true will return the element.
xpath
//input[@type=’text’ or @type=’email’]
These logical operators are helpful when filtering elements based on multiple attributes.
π 5. XPath parent in Selenium
Master XPath in Selenium: The parent function helps retrieve the immediate parent of the current element.
β Syntax:
xpath
//span[text()=’Click Here’]/parent::div
Useful when child elements are known, but you need to access their containers or wrappers.
π 6. XPath starts-with() in Selenium
The starts-with() function is used to match elements whose attribute values begin with a specific string.
β Syntax:
xpath
//input[starts-with(@id, ‘user’)]
Helps when dealing with dynamic attributes that change but maintain a consistent prefix.
π 7. XPath Axes in Selenium
XPath axes help navigate complex DOM structures by targeting elements based on their relationship with another element.
following
Finds the immediate element after the current one.
xpath
//input[@name=’email’]/following::input
preceding
Finds elements that appear before the current element.
xpath
//button[@id=’submit’]/preceding::input
descendant
Finds all levels of children beneath an element.
xpath
//div[@id=’main’]/descendant::span
π Real-Time Use Case Example
β Scenario:
You want to locate all elements in the “Popular Courses” section that are:
- Siblings of the element containing text SELENIUM
- Or descendants of its parent
- Or dynamically located using starts-with and XPath axes
β Sample XPath:
xpath
//a[contains(text(), ‘SELENIUM’)]/following-sibling::*
//a[text()=’SELENIUM’]/ancestor::div//*
π Summary
When basic XPath fails to locate dynamic or nested elements, advanced XPath functions come to the rescue.
β Key Functions:
- contains() β partial matching of text/attributes
- following-sibling β navigate to next sibling element
- ancestor β trace back to parent hierarchy
- AND, OR β apply multiple conditions
- parent β access immediate parent
- starts-with() β useful for dynamic attributes
- XPath Axes (following, preceding, descendant) β handle complex DOM structures
Mastering these techniques allows for precise and flexible element location, even on the most dynamic web applications.