0
0
Selenium Javatesting~20 mins

XPath with text() in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XPath Text Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2: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>
A//button[text()='submit']
B//button[contains(text(),'Submit')]
C//button[text()='Submit']
D//button[text()='Submit Form']
Attempts:
2 left
💡 Hint
Use text() to match exact visible text, case-sensitive.
assertion
intermediate
2: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']"));
AassertEquals(element.getText(), "Welcome");
BassertTrue(element.isDisplayed());
CassertEquals(element.getText(), "welcome");
DassertFalse(element.isDisplayed());
Attempts:
2 left
💡 Hint
Check both visibility and exact text match with correct case.
Predict Output
advanced
2: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>
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
normalize-space() removes leading/trailing spaces and collapses multiple spaces.
🔧 Debug
advanced
2:00remaining
Debug the failing XPath locator using text()
A test fails to locate the element with text 'Cancel' using the XPath //button[text()='Cancel']. The HTML is:
<button> Cancel </button>

What is the most likely reason for failure?
AXPath syntax is incorrect; missing parentheses.
BExtra spaces inside the button text cause exact text() match to fail.
CThe button element is not visible on the page.
DThe text 'Cancel' is inside a child span element, not directly in button.
Attempts:
2 left
💡 Hint
Check for whitespace differences in visible text.
framework
expert
2: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?
A//span[text()[contains(.,'Error')]]
B//span[text()='Error']
C//span[starts-with(text(),'Error')]
D//span[contains(text(),'Error')]
Attempts:
2 left
💡 Hint
Use contains() to match partial text anywhere inside the element.