By Shivam Singh, founder of TesterRank · Updated September 2026
Think of the page as a family tree. Every element has a parent, may have children, and may have brothers and sisters (siblings). An axis is simply the direction you walk in that tree. You use axes when the element you want has no good locator, but something close to it does — like a label, a row title or a product name.
XPath axes let you navigate the DOM tree in any direction — up to a parent, down to a child, sideways to a sibling. This is what makes XPath more powerful than CSS selectors. CSS can only go down (child/descendant) and sideways to the next sibling. XPath can go everywhere.
An axis is a direction to move in the page tree. parent:: goes up one level; ancestor:: keeps going up until it finds a match. CSS cannot go up at all.
// parent:: — go up ONE level
driver.findElement(By.xpath(
"//span[@class='error-message']/parent::div"));
// Find the div that contains an error message
driver.findElement(By.xpath(
"//button[@data-testid='loginBtn']/parent::form"));
// Find the form that contains the login button
// ancestor:: — go up to ANY ancestor (parent, grandparent, etc.)
driver.findElement(By.xpath(
"//span[@data-testid='product-name-1']/ancestor::div[@data-testid='product-item']"));
// From product name → go up to the product cardSiblings are elements that share the same parent. These axes find the element right after or before another one, like the input next to a label.
// following-sibling:: — find siblings AFTER this element
driver.findElement(By.xpath(
"//label[text()='Password']/following-sibling::div//input"));
// Find the input that comes after the "Password" label
// preceding-sibling:: — find siblings BEFORE this element
driver.findElement(By.xpath(
"//input[@id='password']/preceding-sibling::label"));
// Find the label before the password input
// CSS can do following sibling (+) but NOT preceding sibling
// This is a major reason to use XPathThese axes move down the tree, or forward and back through the page. You will use them less often, but they are handy for tricky layouts.
// child:: — direct children only
driver.findElements(By.xpath(
"//div[@data-testid='product-list']/child::div"));
// descendant:: — children at ANY depth
driver.findElements(By.xpath("//form//descendant::input"));
// following:: — ANY element after this one (not just siblings)
driver.findElement(By.xpath(
"//h2[text()='Recent Transactions']/following::table"));
// preceding:: — ANY element before this one
driver.findElement(By.xpath("//table/preceding::h2"));Square brackets with a number pick one match by position. Remember: XPath starts counting at 1, not 0.
// XPath indexes are 1-based (not 0-based like CSS)
// Get the 2nd product card
driver.findElement(By.xpath(
"(//div[@data-testid='product-item'])[2]"));
// Get the LAST product card
driver.findElement(By.xpath(
"(//div[@data-testid='product-item'])[last()]"));
// Get the first 3 table rows
driver.findElements(By.xpath("//table//tr[position()<=3]"));
// Get the last row
driver.findElement(By.xpath("//table//tr[last()]"));These examples combine axes on the TesterRank portals, such as finding the price that belongs to a product name.
// Banking Portal
// Find the form that contains the Sign In button
driver.findElement(By.xpath(
"//button[text()='Sign In']/ancestor::form"));
// Find the "View Statement" link on dashboard
driver.findElement(By.xpath("//a[text()='View Statement']"));
// Find Fund Transfer quick action by its label
driver.findElement(By.xpath(
"//p[text()='Fund Transfer']/ancestor::a"));
// Get a specific column from a transaction row
driver.findElement(By.xpath(
"//tr[@data-testid='txnRow-0']/td[2]"));
// Shopping Portal
// Find Add to Cart button for a specific product
driver.findElement(By.xpath(
"//span[contains(text(), 'Phone')]/ancestor::div[@data-testid='product-item']//button[contains(@class, 'add-to-cart')]"));
// Find all in-stock products
driver.findElements(By.xpath(
"//span[text()='In Stock']/ancestor::div[@data-testid='product-item']"));The most useful XPath pattern in real projects: start from a text you can see → go UP to the container with ancestor:: → go DOWN to the element you need. Like: //span[text()="Phone"]/ancestor::div[@class="card"]//button — find the button inside the card that contains "Phone".
The most common real use is tables: find the row by a value you know, then move to the cell or button you need. For example, "find the row that contains transaction ID 1042, then click its View button". This is far more stable than "click the button in row 3", because the row can move but the transaction ID stays with it.
| Axis | Direction | Typical use |
|---|---|---|
| parent:: | One level up | From a label to its wrapper |
| ancestor:: | Any level up | From a cell to its whole row (tr) |
| following-sibling:: | Next elements at same level | From a label to its input |
| preceding-sibling:: | Earlier elements at same level | From a price to the product name before it |
| descendant:: | Any level down | From a card to a button inside it |
Keep axis chains short. One or two steps is readable; five steps is a sign the page needs a better locator.
Key Point: XPath axes let you go up (parent/ancestor), down (child/descendant), and sideways (sibling). CSS can only go down and to the next sibling.