Discover how to find tricky webpage elements easily by understanding their family tree!
Why XPath axes (parent, following-sibling, preceding) in Selenium Java? - Purpose & Use Cases
Imagine you are testing a complex webpage with many nested elements. You want to find a button next to a specific label, but the button does not have a unique ID or class. You try to click it manually by searching through the page's HTML structure line by line.
Manually searching for elements by guessing their position is slow and error-prone. You might click the wrong button or miss it entirely. Also, if the page layout changes slightly, your manual method breaks and you have to start over.
XPath axes like parent, following-sibling, and preceding let you navigate the HTML tree precisely. You can find elements relative to others, making your tests more reliable and easier to write.
driver.findElement(By.id("button1")).click(); // Fails if ID missing or changed
driver.findElement(By.xpath("//label[text()='Name']/following-sibling::button")).click();You can write smart, flexible tests that find elements based on their relationship to others, not just fixed IDs or classes.
On a form, you want to click the 'Submit' button that comes right after the 'Terms and Conditions' checkbox label, even if the button has no unique ID.
Manual element search is slow and breaks easily.
XPath axes let you navigate element relationships precisely.
This makes tests more reliable and easier to maintain.
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
