XPath axes help you find elements related to a known element in a web page. They make locating elements easier when you know their position relative to others.
XPath axes (parent, following-sibling, preceding) in Selenium Java
xpath_expression/axis_name::node_test
Replace axis_name with parent, following-sibling, or preceding-sibling.
node_test usually specifies the tag name or * for any node.
div of the element with id 'child'.//div[@id='child']/parent::divli elements that come after the current li with class 'item' on the same level.//li[@class='item']/following-sibling::liinput elements that come before the span with class 'label' on the same level.//span[@class='label']/preceding-sibling::input
This Java Selenium code opens a test page, finds a child element by ID, then uses XPath axes to find its parent, following sibling, and preceding sibling elements. It prints their tag names or IDs.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class XPathAxesExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/testpage"); // Find child element WebElement child = driver.findElement(By.id("childElement")); // Find parent of the child element WebElement parent = child.findElement(By.xpath("parent::div")); System.out.println("Parent tag name: " + parent.getTagName()); // Find following sibling of the child element WebElement followingSibling = child.findElement(By.xpath("following-sibling::div")); System.out.println("Following sibling id: " + followingSibling.getAttribute("id")); // Find preceding sibling of the child element WebElement precedingSibling = child.findElement(By.xpath("preceding-sibling::div")); System.out.println("Preceding sibling id: " + precedingSibling.getAttribute("id")); } finally { driver.quit(); } } }
Always ensure the XPath axes match the actual HTML structure to avoid errors.
Using axes can make your locators more flexible and robust when element positions change.
XPath axes help navigate between related elements in the page.
Common axes are parent, following-sibling, and preceding-sibling.
They improve locating elements when direct attributes are missing.
Practice
Solution
Step 1: Understand XPath axes purpose
XPath axes define relationships between nodes, like parent or siblings.Step 2: Identify axis for immediate parent
Theparent::axis selects the direct parent node of the current element.Final Answer:
parent:: -> Option DQuick Check:
Parent axis = parent:: [OK]
- Confusing parent:: with child:: axis
- Using following-sibling:: to select parent
- Using preceding:: which selects previous nodes, not parent
Solution
Step 1: Recall correct axis syntax for following siblings
The axis for following siblings isfollowing-sibling::and it requires a node test like*for all siblings.Step 2: Check each option syntax
//div/following-sibling::* usesfollowing-sibling::*which correctly selects all following siblings. Others are incorrect syntax or wrong axis.Final Answer:
//div/following-sibling::* -> Option CQuick Check:
Following siblings = following-sibling::* [OK]
- Omitting '::' after axis name
- Using 'following::sibling' which is invalid
- Confusing preceding-sibling with following-sibling
<ul> <li id='item1'>First</li> <li id='item2'>Second</li> <li id='item3'>Third</li> </ul>
What text will this XPath select?
//li[@id='item1']/following-sibling::li[1]/text()Solution
Step 1: Understand the XPath expression
The XPath selects the firstlielement with id 'item1', then selects its first following siblinglielement, and gets its text.Step 2: Identify the first following sibling of 'item1'
The first following siblingliafter 'item1' is theliwith id 'item2' which contains text 'Second'.Final Answer:
Second -> Option AQuick Check:
Following sibling of item1 is item2 = Second [OK]
- Selecting the current node text instead of sibling
- Confusing preceding siblings with following siblings
- Assuming index [1] means first child, not sibling
//div[@class='menu']/preceding-sibling::div[2]What is wrong?
Solution
Step 1: Understand XPath indexing with axes
When using an index with axes likepreceding-sibling::div, the index applies to the node set returned by the axis, so parentheses are needed.Step 2: Correct the XPath syntax
The correct syntax to select the second preceding sibling div is(preceding-sibling::div)[2], notpreceding-sibling::div[2].Final Answer:
The index should be inside parentheses like (preceding-sibling::div)[2] -> Option BQuick Check:
Index with axes needs parentheses [OK]
- Placing index directly after node test without parentheses
- Assuming axes do not support indexing
- Confusing predicate indexing with axis indexing
div of a span element that has a class 'highlight', but only if the parent div has a following sibling div with class 'footer'. Which XPath expression correctly achieves this?Solution
Step 1: Understand the requirement
Select the parentdivof aspanwith class 'highlight', but only if that parentdivhas a following siblingdivwith class 'footer'.Step 2: Analyze each XPath option
//span[@class='highlight']/parent::div[following-sibling::div[@class='footer']] selects the parentdivof thespanand filters it by checking if it has a following siblingdivwith class 'footer', which matches the requirement exactly.Final Answer:
//span[@class='highlight']/parent::div[following-sibling::div[@class='footer']] -> Option AQuick Check:
Parent with following sibling filter = //span[@class='highlight']/parent::div[following-sibling::div[@class='footer']] [OK]
- Selecting following sibling instead of filtering parent
- Using preceding-sibling instead of following-sibling
- Misplacing axes order causing wrong node selection
Ready to prove it?
Pass the test and this pattern moves unknown
