XPath

XPath image
XPath, short for XML Path Language, is used to navigate and locate elements within XML and HTML documents. In software testing, especially in automation, XPath becomes handy when elements cannot be found using simple locators like ID or class name.

XPath offers two types of expressions.

An absolute XPath begins from the root and follows a full path like

html/body/div[2]/div[1]/form/input

which makes it more rigid and prone to break when the DOM changes.

A relative XPath, on the other hand, starts from a specific node and is easier to maintain. For example,
 
//form[@id='loginForm']/input[@type='text'] 

is a cleaner and more adaptable locator.

XPath Axes : XPath also allows moving through the DOM tree in different directions using axes. These axes help locate elements based on their relationships.
  • child selects direct children, such as //div[@id='menu']/child::ul
  • parent moves to the parent of a node, like //label[@for='username']/parent::div
  • ancestor looks up through the hierarchy, for example //span[@class='highlight']/ancestor::section
  • descendant finds all levels of children under a node, like //div[@class='container']/descendant::a
  • following selects elements after the current one, such as //h2[text()='Title']/following::p
  • preceding goes to elements before the current node, for example //input[@id='search']/preceding::label
  • following-sibling selects siblings that come next, like //li[@class='active']/following-sibling::li
  • preceding-sibling goes to earlier siblings, for example //li[@class='current']/preceding-sibling::li

Challenges
  • XPath can be powerful but also tricky.
  •  When the DOM changes frequently, XPath locators may break easily.
  •  Absolute paths can become long and hard to maintain.
  •  Sometimes elements have dynamic attributes like auto-generated IDs, which make XPath writing a bit more complex.
  •  Choosing the wrong axis or writing overly specific paths can lead to fragile scripts.

Dynamic XPath: Dynamic XPath makes locators more flexible using functions.
  • contains() helps match partial values, like //button[contains(@class,'submit')]
  • starts-with() is useful for prefixes, such as //input[starts-with(@id,'user')]
  • text() finds elements using visible content, for example //a[text()='Login']
  • normalize-space() removes extra spaces, like //label[normalize-space(text())='Username']

These strategies improve locator resilience and readability, especially in modern web apps where elements and structures shift often.

XPath vs Other Locators in Automation :  While XPath is a powerful and flexible locator, it’s not always the best tool for every situation. Understanding when to use it over other locators can make test automation more efficient and reliable.

1. XPath vs ID

  • ID is the fastest and most reliable locator when available.
  • Browsers optimize element search by ID.
  • XPath should be used only when the ID is missing, dynamic, or duplicated.

Example:
ID: driver.findElement(By.id("username"))
XPath: //input[@id='username']

Conclusion:
Use ID when possible. It’s cleaner and quicker.

2. XPath vs CSS Selectors

  • CSS Selectors are typically faster in execution and easier to read.
  • But XPath can travel both ways in the DOM — parent to child and child to parent — while CSS can only move forward.
  • XPath supports more complex logic using functions and axes.

Example:
CSS: form#loginForm input[type='text']
XPath: //form[@id='loginForm']/input[@type='text']

Conclusion : Use CSS for speed and readability, but prefer XPath for advanced structure navigation.

3. XPath vs Class Name

  • Class Name is fine when the class is unique or specific enough.
  • If the class is shared among many elements, it becomes unreliable.
  • XPath allows filtering multiple attributes and conditions for better precision.

Example:
Class: driver.findElement(By.className("submit-btn"))
XPath: //button[contains(@class, 'submit-btn')]

Conclusion : Use class names for simplicity, XPath for more control.

4. XPath vs Name

  • Name is useful when fields like forms or inputs have clearly defined name attributes.
  • However, name attributes are often missing in modern dynamic pages.

Example:
Name: driver.findElement(By.name("email"))
XPath: //input[@name='email']

Conclusion : Use name when available, fallback on XPath for complex cases.

Leave Complexity Behind image
No-Code Test Automation with Low-Code integration via Robot Framework and TestBench
Explore MoT
Leading with Quality image
Tue, 30 Sep
A one-day educational experience to help business lead with expanding quality engineering and testing practices.
MoT Software Testing Essentials Certificate image
Boost your career in software testing with the MoT Software Testing Essentials Certificate. Learn essential skills, from basic testing techniques to advanced risk analysis, crafted by industry experts.
Leading with Quality
A one-day educational experience to help business lead with expanding quality engineering and testing practices.
This Week in Testing image
Debrief the week in Testing via a community radio show hosted by Simon Tomes and members of the community
Subscribe to our newsletter
We'll keep you up to date on all the testing trends.