The following-sibling axis selects siblings after the current node.
✗ Incorrect
Option D correctly selects the immediate next sibling li after 'Item 2'. Option D selects the previous sibling, C selects a child by position, and D selects the second following li which is 'Item 4'.
❓ Predict Output
advanced
2:00remaining
What is the result of this XPath query?
Given the HTML below, what text will be selected by the XPath expression //h2[text()='Chapter 2']/preceding-sibling::h2[1]?
Option B correctly uses parent::div to find the parent div and asserts its class attribute equals 'product'. Other options use incorrect axes or methods.
🔧 Debug
expert
3:00remaining
Debug the failing XPath locator using axes
A test fails because the XPath //div[@class='menu']/following-sibling::ul/li does not find any elements. Given the HTML below, what is the main reason for failure?
AThe <ul> is not a following sibling of the <div>, it is a following node but not sibling.
BThe XPath should use preceding-sibling instead of following-sibling.
CThe <ul> is a child of the <div>, so child axis should be used.
DThe XPath syntax is invalid because axes cannot be used after attribute filters.
Attempts:
2 left
💡 Hint
Check the HTML structure to see sibling relationships.
✗ Incorrect
The
is not a sibling of the
but a following node at the same level, so following-sibling axis does not select it. Option A is incorrect; the XPath syntax is valid. Option A is wrong because preceding-sibling is the opposite direction. Option A is wrong because
is not a child of
.
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.