The following-sibling axis selects siblings after the current node. The [1] means the first following sibling.
✗ Incorrect
The XPath selects the first
element that is a following sibling of the
with id 'title'. That is the
with text 'First paragraph.'.
❓ assertion
advanced
2:00remaining
Assertion for sibling element text
You want to assert that the paragraph immediately following the
with id 'header' contains the text 'Welcome!'. Which assertion is correct in Python Selenium?
Selenium Python
element = driver.find_element(By.XPATH, "//h2[@id='header']/following-sibling::p[1]")
actual_text = element.text
Aassert actual_text == 'Welcome!'
Bassert actual_text != 'Welcome!'
Cassert 'Welcome!' in driver.find_element(By.XPATH, "//h2[@id='header']/parent::p").text
Dassert element.text == 'header'
Attempts:
2 left
💡 Hint
Check that the text of the selected sibling matches exactly 'Welcome!'.
✗ Incorrect
Option A correctly asserts that the text of the first following sibling
is exactly 'Welcome!'. Option A asserts the opposite. Option A uses parent axis incorrectly and will raise an error. Option A compares text to 'header' which is incorrect.
🔧 Debug
expert
3:00remaining
Debugging XPath parent axis usage
The following Selenium code raises a NoSuchElementException. What is the most likely cause?
Selenium Python
element = driver.find_element(By.XPATH, "//span[@class='label']/parent::div[@class='container']")
ASelenium does not support the parent axis in XPath.
BThe parent axis cannot be used with predicates like [@class='container'].
CThe <span> element does not have a parent <div> with class 'container'.
DThe XPath syntax for parent axis is incorrect; it should be 'parent:div'.
Attempts:
2 left
💡 Hint
Check the HTML structure to confirm the parent element's class attribute.
✗ Incorrect
Option C is correct because if the parent
does not have the class 'container', the XPath will not find any matching element, causing NoSuchElementException. Option C is wrong syntax; 'parent::' is correct. Option C is false; predicates can be used with axes. Option C is false; Selenium supports parent axis.
Practice
(1/5)
1. Which XPath axis selects the direct parent of the current node?
easy
A. child::
B. parent::
C. following-sibling::
D. preceding-sibling::
Solution
Step 1: Understand XPath axes
XPath axes describe relationships between nodes, such as parent, child, or siblings.
Step 2: Identify the axis for parent node
The parent:: axis selects the direct parent of the current node.
Final Answer:
parent:: -> Option B
Quick Check:
Parent axis = parent:: [OK]
Hint: Parent axis always uses 'parent::' [OK]
Common Mistakes:
Confusing parent:: with child::
Using sibling axes to find parent
Forgetting axes syntax with double colons
2. Which of the following is the correct XPath syntax to select all child <li> elements of a <ul> element?
easy
A. //ul/parent::li
B. //ul/preceding-sibling::li
C. //ul/following-sibling::li
D. //ul/child::li
Solution
Step 1: Understand child axis usage
The child:: axis selects direct children of the current node.
Step 2: Apply correct syntax for child li elements
Using //ul/child::li selects all li children of ul.
Final Answer:
//ul/child::li -> Option D
Quick Check:
Child axis syntax = child:: [OK]
Hint: Use 'child::' to select direct children nodes [OK]
What does the XPath //h2/following-sibling::p[1] select?
medium
A. No element, XPath is invalid
B. The second p element inside the div
C. The first p element inside the div
D. The h2 element itself
Solution
Step 1: Understand following-sibling axis
The following-sibling::p[1] selects the first p sibling after the h2 node.
Step 2: Apply to given HTML
After h2, the first p is "Paragraph 1", so it selects that element.
Final Answer:
The first <p> element inside the <div> -> Option C
Quick Check:
following-sibling::p[1] = first p after h2 [OK]
Hint: following-sibling::p[1] picks first p after current node [OK]
Common Mistakes:
Selecting the second p instead of the first
Confusing preceding-sibling with following-sibling
Thinking it selects children instead of siblings
4. You wrote this XPath to select the previous sibling of a div element: //div/preceding-sibling::div[1] But it returns no elements. What is the most likely reason?
medium
A. The div has no preceding sibling div elements
B. The syntax is incorrect; axes cannot be used with //
C. You should use parent::div instead
D. preceding-sibling axis selects children, not siblings
Solution
Step 1: Understand preceding-sibling axis
The preceding-sibling::div[1] selects the immediate previous sibling div element.
Step 2: Reason why no elements returned
If no elements are returned, it means the current div has no preceding sibling div.
Final Answer:
The <div> has no preceding sibling <div> elements -> Option A
Quick Check:
preceding-sibling returns nothing if no previous siblings [OK]
Hint: No preceding sibling means empty result, not syntax error [OK]
Common Mistakes:
Thinking axes can't be used with //
Confusing parent:: with preceding-sibling::
Assuming preceding-sibling selects children
5. You want to select all span elements that are children of a div whose immediate parent is a section. Which XPath expression correctly uses axes to achieve this?
hard
A. //section/child::div/child::span
B. //div/parent::section/child::span
C. //section/parent::div/child::span
D. //span/parent::div/parent::section
Solution
Step 1: Understand the hierarchy
The span elements are children of div, and that div is a child of section.
Step 2: Build XPath with axes
//section/child::div/child::span selects span children of div children of section.
Step 3: Check other options
//div/parent::section/child::span selects span children of section, which is incorrect. //section/parent::div/child::span and D misuse axes or order.
Final Answer:
//section/child::div/child::span -> Option A
Quick Check:
Use child axes from section to div to span [OK]
Hint: Chain child:: axes from parent to target child [OK]
Common Mistakes:
Using parent:: in wrong direction
Selecting wrong node levels
Mixing sibling axes instead of child axes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.