Challenge - 5 Problems
XPath Text Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate2:00remaining
Identify the correct XPath locator using text()
Given the HTML snippet below, which XPath expression correctly selects the
Selenium Java
<button>Submit</button> <button>submit</button> <button>Submit Form</button>
Attempts:
2 left
💡 Hint
Use text() to match exact visible text, case-sensitive.
✗ Incorrect
Option C uses text() to match the exact text 'Submit'. Option C matches any button containing 'Submit' but would also match 'Submit Form'. Option C is case-sensitive and does not match 'Submit'. Option C matches a different button with text 'Submit Form'.
❓ assertion
intermediate2:00remaining
Assertion on element text using XPath with text()
In Selenium Java, which assertion correctly verifies that the element located by XPath
//div[text()='Welcome'] has the text 'Welcome'?Selenium Java
WebElement element = driver.findElement(By.xpath("//div[text()='Welcome']"));Attempts:
2 left
💡 Hint
Check both visibility and exact text match with correct case.
✗ Incorrect
Option A correctly asserts the element's text is exactly 'Welcome'. Option A checks visibility but not text. Option A fails due to case mismatch. Option A asserts the element is not displayed, which is incorrect.
❓ Predict Output
advanced2:00remaining
Output of XPath expression with text() and normalize-space()
What is the output count of elements selected by the XPath
//p[normalize-space(text())='Hello World'] given the HTML below?Selenium Java
<p> Hello World </p> <p>Hello World</p> <p>Hello World!</p> <p> HelloWorld </p>
Attempts:
2 left
💡 Hint
normalize-space() removes leading/trailing spaces and collapses multiple spaces.
✗ Incorrect
The first two
elements normalize to 'Hello World' and match exactly. The third has an exclamation mark, the fourth has no space between words, so only 2 match.
🔧 Debug
advanced2:00remaining
Debug the failing XPath locator using text()
A test fails to locate the element with text 'Cancel' using the XPath
What is the most likely reason for failure?
//button[text()='Cancel']. The HTML is:<button> Cancel </button>
What is the most likely reason for failure?
Attempts:
2 left
💡 Hint
Check for whitespace differences in visible text.
✗ Incorrect
The button text has spaces around 'Cancel', so exact text() match fails. Using normalize-space() would fix this. Options B, C, and D do not match the given HTML or XPath.
❓ framework
expert2:00remaining
Best practice for dynamic text matching in XPath locators
In a Selenium Java framework, which XPath locator is best to find a
<span> element containing dynamic text that always includes the word 'Error' somewhere inside it?Attempts:
2 left
💡 Hint
Use contains() to match partial text anywhere inside the element.
✗ Incorrect
Option D correctly uses contains(text(),'Error') to match any span with 'Error' in its text. Option D matches exact text 'Error' only. Option D matches text starting with 'Error' only. Option D uses advanced text node filtering but is more complex than needed.