The parent:: axis selects the parent node of the current node in the XML or HTML document tree.
Click to reveal answer
beginner
How does following-sibling:: axis work in XPath?
The following-sibling:: axis selects all sibling nodes that come after the current node at the same tree level.
Click to reveal answer
intermediate
What is the difference between preceding:: and preceding-sibling:: axes?
preceding:: selects all nodes that come before the current node in the document, regardless of level, while preceding-sibling:: selects only sibling nodes before the current node at the same level.
Click to reveal answer
beginner
Write an XPath to select the parent of a <span> element with id 'price'.
XPath: //span[@id='price']/parent::* selects the parent node of the <span> with id 'price'.
Click to reveal answer
intermediate
In Selenium Java, how would you locate the next sibling <div> after a <h2> element with text 'Title'?
Use XPath: //h2[text()='Title']/following-sibling::div[1] to find the first <div> sibling after the <h2> with text 'Title'.
Click to reveal answer
Which XPath axis selects the parent node of the current node?
Apreceding::
Bparent::
Cfollowing-sibling::
Dchild::
✗ Incorrect
The parent:: axis selects the parent node of the current node.
What does following-sibling::div[1] select?
AThe first <code><div></code> sibling after the current node
BThe first <code><div></code> sibling before the current node
CThe parent <code><div></code> node
DAll <code><div></code> descendants
✗ Incorrect
It selects the first <div> sibling that comes after the current node.
Which axis selects all nodes before the current node regardless of level?
Apreceding::
Bfollowing-sibling::
Cpreceding-sibling::
Dparent::
✗ Incorrect
preceding:: selects all nodes before the current node in the document, regardless of their position in the tree.
In XPath, how do you select the parent of the current node?
Achild::*
Bfollowing::*
Cancestor::*
Dparent::*
✗ Incorrect
parent::* selects the parent node of the current node.
Which XPath axis would you use to find siblings before the current node?
Aparent::
Bfollowing-sibling::
Cpreceding-sibling::
Dchild::
✗ Incorrect
preceding-sibling:: selects sibling nodes that come before the current node.
Explain how the XPath axes parent::, following-sibling::, and preceding:: differ in selecting nodes.
Think about the direction and level of nodes each axis selects.
You got /3 concepts.
Write an example XPath using following-sibling:: to select the next sibling <li> after a <li> with class 'active'.
Use the axis with an index to get the first following sibling.
You got /2 concepts.
Practice
(1/5)
1. Which XPath axis selects the immediate parent of the current node?
easy
A. following-sibling::
B. child::
C. preceding::
D. parent::
Solution
Step 1: Understand XPath axes purpose
XPath axes define relationships between nodes, like parent or siblings.
Step 2: Identify axis for immediate parent
The parent:: axis selects the direct parent node of the current element.
What text will this XPath select? //li[@id='item1']/following-sibling::li[1]/text()
medium
A. Second
B. No text selected
C. Third
D. First
Solution
Step 1: Understand the XPath expression
The XPath selects the first li element with id 'item1', then selects its first following sibling li element, and gets its text.
Step 2: Identify the first following sibling of 'item1'
The first following sibling li after 'item1' is the li with id 'item2' which contains text 'Second'.
Final Answer:
Second -> Option A
Quick Check:
Following sibling of item1 is item2 = Second [OK]
Hint: following-sibling::li[1] selects immediate next sibling [OK]
Common Mistakes:
Selecting the current node text instead of sibling
Confusing preceding siblings with following siblings
Assuming index [1] means first child, not sibling
4. Identify the error in this XPath expression: //div[@class='menu']/preceding-sibling::div[2] What is wrong?
medium
A. preceding-sibling axis cannot use index
B. The index should be inside parentheses like (preceding-sibling::div)[2]
C. XPath axes cannot be used after predicates
D. No error, expression is correct
Solution
Step 1: Understand XPath indexing with axes
When using an index with axes like preceding-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], not preceding-sibling::div[2].
Final Answer:
The index should be inside parentheses like (preceding-sibling::div)[2] -> Option B
Quick Check:
Index with axes needs parentheses [OK]
Hint: Use parentheses around axis before indexing [OK]
Common Mistakes:
Placing index directly after node test without parentheses
Assuming axes do not support indexing
Confusing predicate indexing with axis indexing
5. You want to select the parent 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?
hard
A. //span[@class='highlight']/parent::div[following-sibling::div[@class='footer']]
B. //span[@class='highlight']/parent::div/following-sibling::div[@class='footer']
C. //span[@class='highlight']/preceding::div[@class='footer']/parent::div
D. //span[@class='highlight']/parent::div[preceding-sibling::div[@class='footer']]
Solution
Step 1: Understand the requirement
Select the parent div of a span with class 'highlight', but only if that parent div has a following sibling div with class 'footer'.
Step 2: Analyze each XPath option
//span[@class='highlight']/parent::div[following-sibling::div[@class='footer']] selects the parent div of the span and filters it by checking if it has a following sibling div with class 'footer', which matches the requirement exactly.
Final Answer:
//span[@class='highlight']/parent::div[following-sibling::div[@class='footer']] -> Option A
Quick Check:
Parent with following sibling filter = //span[@class='highlight']/parent::div[following-sibling::div[@class='footer']] [OK]
Hint: Filter parent with predicate checking following sibling [OK]
Common Mistakes:
Selecting following sibling instead of filtering parent
Using preceding-sibling instead of following-sibling
Misplacing axes order causing wrong node selection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.