Recall & Review
beginner
What does the XPath function
text() do?The
text() function in XPath selects the text content of an element. It helps locate elements based on their visible text.Click to reveal answer
beginner
Write an XPath to find a button with exact text 'Submit'.
Use
//button[text()='Submit'] to find a button element whose text is exactly 'Submit'.Click to reveal answer
intermediate
How to find an element containing text 'Login' anywhere inside it using XPath?
Use
//tag[contains(text(),'Login')] where tag is the element type, e.g., //div[contains(text(),'Login')].Click to reveal answer
beginner
Why is using
text() in XPath useful in Selenium tests?It allows selecting elements by their visible text, which is often stable and user-friendly, making tests easier to read and maintain.
Click to reveal answer
intermediate
What is a limitation of using
text() in XPath?It matches only direct text nodes, so if the text is split by child elements or contains extra spaces, the XPath may not find the element.
Click to reveal answer
Which XPath expression finds a
span element with exact text 'Hello'?✗ Incorrect
The correct syntax to match exact text is
//span[text()='Hello']. Option B matches partial text, A and D are invalid.What does
//div[contains(text(),'Error')] select?✗ Incorrect
The
contains(text(),'Error') function selects elements whose text includes 'Error' anywhere.Why might
//p[text()='Welcome'] fail to find a paragraph?✗ Incorrect
If the text is split or has extra spaces, exact match with
text() may fail.Which is the best XPath to find a button with text containing 'Save'?
✗ Incorrect
Using
contains(text(),'Save') finds buttons with text that includes 'Save' anywhere.In Selenium Java, how do you locate an element with XPath using text()?
✗ Incorrect
The correct way is to use
By.xpath with the XPath expression including text().Explain how to use XPath with
text() to find elements by their visible text in Selenium.Think about how you find a button or label by the words you see on the screen.
You got /5 concepts.
What are some challenges or limitations when using XPath with
text() in test automation?Consider how the text might look different in HTML than what you see.
You got /4 concepts.