0
0
Selenium Javatesting

XPath with text() in Selenium Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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'?
A//span[text()='Hello']
B//span[contains(text(),'Hello')]
C//span[@text='Hello']
D//span[text()*='Hello']
What does //div[contains(text(),'Error')] select?
AAll <code>div</code> elements containing the word 'Error' anywhere in their text
BAll <code>div</code> elements with text exactly 'Error'
CAll elements with attribute text='Error'
DAll <code>div</code> elements with child elements named 'Error'
Why might //p[text()='Welcome'] fail to find a paragraph?
ABecause <code>text()</code> only matches attributes
BBecause the paragraph text might have extra spaces or child elements splitting the text
CBecause <code>text()</code> is case insensitive
DBecause <code>text()</code> matches only numbers
Which is the best XPath to find a button with text containing 'Save'?
A//button[text()*='Save']
B//button[text()='Save']
C//button[@text='Save']
D//button[contains(text(),'Save')]
In Selenium Java, how do you locate an element with XPath using text()?
Adriver.findElement(By.cssSelector("div[text()='Example']"))
Bdriver.findElement(By.id("text()='Example'"))
Cdriver.findElement(By.xpath("//div[text()='Example']"))
Ddriver.findElement(By.name("text()='Example'"))
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.