Challenge - 5 Problems
XPath Axes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate2:00remaining
Locate the parent element using XPath axes
Given the following HTML snippet, which XPath expression correctly selects the
element that is the parent of the element with text 'Hello'?
Selenium Python
<div class="container"> <span>Hello</span> </div>
Attempts:
2 left
💡 Hint
Remember that the parent axis selects the immediate parent node of the current node.
✗ Incorrect
Option D uses the parent axis to select the
that is the parent of the with text 'Hello'. Option D selects the child of a
, not the parent. Option D tries to select a child
of , which does not exist. Option D incorrectly reverses the parent-child relationship.
❓ locator
intermediate2:00remaining
Select child elements with XPath axes
Which XPath expression selects all child elements of the
- with id 'menu'?
Selenium Python
<ul id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>Attempts:
2 left
💡 Hint
Child axis selects direct children of the current node.
✗ Incorrect
Option A correctly selects all elements that are direct children of the
- with id 'menu'. Option A tries to select a
- , which is incorrect. Option A selects the parent of
- , which is not
- . Option A selects siblings of
- , which are not
- elements.
- . Option A selects siblings of
- child of
❓ Predict Output
advanced2:00remaining
Output of XPath sibling selection
Given the HTML below, what text will be selected by the XPath expression //h2[@id='title']/following-sibling::p[1]?
Selenium Python
<div>
<h2 id="title">Section</h2>
<p>First paragraph.</p>
<p>Second paragraph.</p>
</div>Attempts:
2 left
💡 Hint
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
advanced2: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.textAttempts:
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
expert3: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']")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.