Why XPath Skills Separate Good Testers from Great Testers
Modern automation teams move fast. Every sprint brings new UI changes, dynamic IDs, and complex DOM structures. Testers who rely only on basic locators like ID and name struggle to keep scripts stable. This is why companies look for testers who understand advanced XPath strategies.
XPath skills help testers locate elements even when the application changes, and this makes automation stable, scalable, and reliable.
Industry reports show that over 78% of QA teams still rely on Selenium WebDriver, and XPath remains the most used locator strategy across web automation frameworks. As UI patterns evolve, XPath mastery becomes even more important. That is why learning XPath Contains and other advanced functions becomes a key skill when you join a Selenium certification course or a Selenium course online.
In this guide, you will learn everything about:
- XPath Contains
- xpath contains with AND/OR
- Parent and ancestor navigation
- Starts-with usage
- XPath axes (following, preceding, sibling, descendant, etc.)
- Real project examples
- Step-by-step usage patterns
By the end of this blog, you will write XPath expressions that work on any website, even with dynamic or unstable DOM structures.
What Is XPath and Why Testers Rely on It?
XPath stands for XML Path Language, and it lets Selenium identify page elements using paths, functions, and hierarchical relationships. XPath works even when:

- Elements have no ID
- Elements have auto-generated IDs
- Elements look identical
- Elements load dynamically
- Elements belong to nested structures
This makes XPath the most flexible locator type in Selenium WebDriver.
XPath is especially useful in automation frameworks built with:
- TestNG
- JUnit
- BDD (Cucumber)
- PyTest + Selenium
- Robot Framework
If you enroll in a Selenium certification course or Selenium course online, XPath mastery is always a major part of the curriculum.
Understanding XPath Contains (Focus Keyword)
The phrase XPath Contains or xpath contains refers to using the contains() function in XPath to identify elements with partial attribute values.
This is the most used XPath feature in test automation.
Why XPath Contains is important
- Helps locate elements with dynamic values
- Useful when IDs are partially changing
- Helps locate long attribute values
- Works perfectly for class names used with multiple words
- Supports fuzzy matching
- Reduces script breakage
Syntax
//tagname[contains(@attribute, 'value')]
Example 1: Button with dynamic ID
//button[contains(@id, 'login')]
Example 2: Element with long class value
//div[contains(@class, 'menu')]
Example 3: Partial text matching
//span[contains(text(), 'Welcome')]
Real Project Example
Suppose your product team updates button IDs dynamically:
btn-login-25489btn-login-85473
Using a normal XPath breaks.
Using XPath Contains ensures your script remains stable.
//button[contains(@id, 'btn-login')]
This approach is taught in every Selenium certification course because industry-grade systems use dynamic attributes frequently.
Using XPath Contains with AND & OR

Advanced testers combine XPath Contains with logical operators.
XPath Contains + AND
Use AND to match multiple conditions at once.
Syntax
//tag[contains(@attribute1,'value1') and contains(@attribute2,'value2')]
Example
//input[contains(@class, 'form') and contains(@type, 'text')]
This is useful when an element has many similar siblings.
XPath Contains + OR
Use OR to match either condition.
Syntax
//tag[contains(@attribute,'value1') or contains(@attribute,'value2')]
Example
//button[contains(text(),'Submit') or contains(text(),'Continue')]
This makes tests flexible when UI teams change button text during A/B testing.
Using Starts-with() in Selenium WebDriver
When attributes begin with predictable text, use the starts-with() function.
Syntax
//tag[starts-with(@attribute,'value')]
Example
//input[starts-with(@name,'user')]
Why Start-with is Useful
- IDs sometimes start with fixed text but end with dynamic numbers
- Helps locate dynamic React.js or Angular components
- Works well when SPAs generate repeating elements
Many enterprise-level websites use JavaScript frameworks that auto-generate IDs. This makes xpath contains and starts-with absolutely essential.
Navigating Parent and Ancestor Nodes in XPath
Sometimes, the element you want to target has no clear attributes. In such cases, testers locate child elements and then move upward to parent.
The Parent Axis
Syntax:
//childElement/parent::tagname
Example: Select parent div of an input
//input[@id='email']/parent::div
Real Use Case
Many UI components keep the actual clickable element within a parent wrapper.
In such cases, direct XPath fails. Parent navigation solves this problem.
ancestor:: Axis
Ancestor moves upward multiple levels.
//span[text()='Total']/ancestor::table
This is useful when:
- Table structures are complex
- Elements belong to nested DIVs
- You need to find a related parent container
XPath Axes in Selenium WebDriver
XPath axes allow you to navigate the DOM horizontally or vertically.
This provides extreme control in finding elements based on relationships.
following:: Axis
Select elements that appear after the current node.
//label[text()='Email']/following::input[1]
preceding:: Axis
Select elements that appear before the current node.
//button[text()='Submit']/preceding::label[1]
following-sibling:: Axis
Select siblings on the same level after the current node.
//label[@id='firstName']/following-sibling::input
preceding-sibling:: Axis
Select siblings before the current node.
//span[@id='price']/preceding-sibling::label
descendant:: Axis
Select nested child elements.
//div[@id='menu']/descendant::a
5.6 child:: Axis
Direct children only.
//ul[@id='list']/child::li
5.7 parent:: Axis
Moves one level up.
//span[@class='error']/parent::div
Practical, Real-World Examples
Modern UI frameworks like React, Angular, and Vue generate dynamic DOMs.
Here are real automation scenarios where XPath Contains is essential.
Example: Dynamic dropdown values
//li[contains(@class, 'dropdown-item')]
6.2 Example: Auto-generated input fields
//input[starts-with(@id,'user_')]
Example: Tables with inconsistent column order
//td[contains(text(),'Price')]/following-sibling::td
Example: Extracting labels and values
//label[contains(text(),'Email')]/following::input[1]
Explaining Advanced XPath Strategies (Step-by-Step)
Step 1: Identify a stable anchor
Find an element that rarely changes.
Example: label text like “Email”.
Step 2: Decide the direction
- Downward → descendant
- Upward → parent or ancestor
- Same level → sibling
- Forward → following
- Backward → preceding
Step 3: Apply XPath Contains for fuzziness
//div[contains(@class,'login')]
Step 4: Combine with AND for precision
//input[contains(@class,'form') and @type='text']
Step 5: Use OR when A/B UI varies
//button[contains(text(),'Login') or contains(text(),'Sign In')]
Step 6: Always validate XPath using browser dev tools
Use:
- Chrome DevTools
- Firefox Inspector
- SelectorsHub
- ChroPath
This helps ensure accuracy.
Why XPath Mastery Helps You Get Better Jobs
Companies expect automation engineers to handle unstable UI.
Recruiters often ask XPath questions like:
- Write XPath using contains
- Navigate to parent element
- Use axes to locate elements
- Handle dynamic IDs using starts-with
Learning this boosts your confidence and improves your interview performance.
Most students who join a Selenium certification course or Selenium course online improve their XPath skills within the first few weeks.
Case Study: How a QA Team Improved Script Stability by 60%
A retail e-commerce company faced automation failures due to dynamic DOM updates.
IDs changed every release.
Testers replaced 40% of their failing locators with:
- XPath Contains
- Starts-with
- following-sibling
- parent
Result:
- 60% fewer locator failures
- 30% faster script maintenance
- Higher automation coverage
XPath skills deliver strong ROI in enterprise automation.
Beginner Mistakes to Avoid When Writing XPath
Do not use absolute XPath
Always avoid:
/html/body/div/table/tr/td
Avoid indexing unless necessary
Use:
(//input[@type='text'])[1]
only when no unique attributes exist.
Avoid too many OR conditions
Use clean, readable expressions.
Best Practices for XPath in Selenium WebDriver
- Prefer relative XPath
- Use XPath Contains for dynamic attributes
- Use starts-with for evolving IDs
- Avoid deeply nested chains
- Use axes only when needed
- Test XPath in browser first
- Keep locators readable
These habits are taught in every Selenium course online at H2K Infosys.
Summary of All XPath Techniques Covered

You learned:
- How to use XPath Contains
- xpath contains with AND/OR
- Using Starts-with
- Parent and ancestor navigation
- All major XPath axes
- Real project examples
- Best practices
- How XPath improves automation stability
XPath Contains remains the most used and most powerful locator strategy in Selenium automation.
Key Takeaways
- XPath Contains helps locate dynamic or complex elements.
- Axes help navigate DOM structures easily.
- Starts-with and parent are essential for modern UI testing.
- XPath mastery improves your automation success rate.
- Selenium certification course training strengthens locator skills.
Conclusion
Take your XPath skills to the next level with practical, real-world training that prepares you for the toughest automation challenges.
Join H2K Infosys today to master Selenium automation with expert-led sessions, hands-on projects, and the confidence to excel in your QA career.






















