Challenge - 5 Problems
XPath Axes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate2:00remaining
Identify the correct XPath using the parent axis
Given the HTML snippet below, which XPath expression correctly selects the
element that is the parent of the with text 'Price'?
Selenium Java
<div class="product"> <span>Price</span> </div>
Attempts:
2 left
💡 Hint
The parent axis selects the direct parent node of the current node.
✗ Incorrect
Option A uses the parent axis to select the div that contains the span with text 'Price'. Other options select incorrect axes or nodes.
❓ locator
intermediate2:00remaining
Select the correct following-sibling element
Which XPath expression selects the element that immediately follows the with text 'Item 2'?
Selenium Java
<ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> </ul>
Attempts:
2 left
💡 Hint
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
advanced2: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]?Selenium Java
<div> <h2>Chapter 1</h2> <h2>Chapter 2</h2> <h2>Chapter 3</h2> </div>
Attempts:
2 left
💡 Hint
preceding-sibling selects siblings before the current node.
✗ Incorrect
The XPath selects the first h2 before 'Chapter 2', which is 'Chapter 1'.
❓ assertion
advanced2:30remaining
Which assertion correctly verifies the presence of the parent element?
In Selenium Java, which assertion correctly verifies that the parent of the element located by
//span[@id='price'] has the class 'product'?Selenium Java
WebElement priceSpan = driver.findElement(By.xpath("//span[@id='price']"));Attempts:
2 left
💡 Hint
Use the parent axis to find the parent element.
✗ Incorrect
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
expert3: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?Selenium Java
<div class="menu">Main Menu</div> <ul> <li>Home</li> <li>About</li> </ul>
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
.