Challenge - 5 Problems
XPath Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Understanding Absolute XPath
Which statement best describes an absolute XPath in Selenium?
Attempts:
2 left
💡 Hint
Think about how the path starts from the very top of the HTML document.
✗ Incorrect
Absolute XPath starts from the root node (html) and follows the full path down to the target element, making it very specific but fragile if the page structure changes.
❓ Predict Output
intermediate1:30remaining
Output of XPath Locator in Selenium
Given the following HTML snippet and XPath, what element text will Selenium find?
Selenium Python
<html><body><div><p>First</p><p>Second</p></div></body></html>
xpath = '/html/body/div/p[2]'Attempts:
2 left
💡 Hint
XPath index starts at 1, so p[2] means the second
element inside
.
✗ Incorrect
The XPath '/html/body/div/p[2]' selects the second
element inside the
, which contains the text 'Second'.
❓ locator
advanced2:00remaining
Choosing the Correct Relative XPath
Which XPath expression correctly selects all elements with a class attribute containing 'search' anywhere in the document?
Attempts:
2 left
💡 Hint
Relative XPath starts with '//' to search anywhere in the document.
✗ Incorrect
Option D uses relative XPath starting with '//' to find all elements anywhere with class containing 'search'. Option D is absolute but unnecessarily long. Option D is invalid because it starts with a single slash but no root node. Option D is invalid XPath syntax.
❓ assertion
advanced1:30remaining
Validating Element Presence Using XPath
Which assertion correctly verifies that an element located by relative XPath exists on the page using Selenium in Python?
Selenium Python
element = driver.find_element(By.XPATH, "//button[@id='submit']")Attempts:
2 left
💡 Hint
find_element throws an exception if not found, so checking for None is safe after catching exceptions.
✗ Incorrect
Option A correctly asserts that the element variable is not None, meaning the element was found. Options A and C compare the element object to True, which is invalid. Option A calls a non-existent method 'exists()'.
🔧 Debug
expert2:30remaining
Debugging a Failing XPath Locator
You have this XPath locator in your Selenium test: '/html/body/div[2]/span/input'. The test fails with NoSuchElementException. Which is the most likely reason?
Attempts:
2 left
💡 Hint
Absolute XPath is fragile and breaks if page layout changes.
✗ Incorrect
Absolute XPath depends on exact page structure. If the page changes, the path may no longer point to the element, causing NoSuchElementException. The lack of ID does not cause this error. Using relative XPath is a good practice but not the cause of failure. The XPath syntax with numbers in brackets is valid.