Bird
Raised Fist0
Selenium Javatesting

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

Choose your learning style10 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.

      Practice

      (1/5)
      1. What does the XPath expression //button[text()='Submit'] select in a web page?
      easy
      A. A button element with the exact visible text 'Submit'
      B. Any element with the attribute text='Submit'
      C. All buttons regardless of their text
      D. Any element containing the word 'Submit' anywhere

      Solution

      1. Step 1: Understand the XPath syntax

        The expression //button selects all button elements in the document.
      2. Step 2: Apply the text() condition

        The predicate [text()='Submit'] filters those buttons to only those whose visible text exactly matches 'Submit'.
      3. Final Answer:

        A button element with the exact visible text 'Submit' -> Option A
      4. Quick Check:

        XPath with text() = exact text match [OK]
      Hint: Use text() for exact visible text match in XPath [OK]
      Common Mistakes:
      • Confusing attribute text with text() function
      • Assuming partial text match without contains()
      • Selecting wrong element types
      2. Which of the following is the correct XPath syntax to select a link (<a>) with visible text exactly 'Home'?
      easy
      A. //a[text()='Home']
      B. //a[contains(text(), 'Home')]
      C. //a[text()='home']
      D. //a[@text='Home']

      Solution

      1. Step 1: Identify exact text match syntax

        The correct syntax for exact text match uses text()='Home' inside brackets.
      2. Step 2: Check each option

        //a[contains(text(), 'Home')] uses contains(), which is partial match, not exact. //a[text()='home'] has wrong case 'home' (case-sensitive). //a[@text='Home'] incorrectly treats text as attribute.
      3. Final Answer:

        //a[text()='Home'] -> Option A
      4. Quick Check:

        Exact text match uses text()='value' [OK]
      Hint: Use text()='ExactText' for precise text matching [OK]
      Common Mistakes:
      • Using contains() when exact match is needed
      • Ignoring case sensitivity in text()
      • Treating text as an attribute
      3. Given the following HTML snippet:
      <div>
        <button>Save</button>
        <button>Cancel</button>
        <button>Save As</button>
      </div>

      What will the XPath //button[text()='Save'] select?
      medium
      A. All three buttons
      B. Both buttons with texts 'Save' and 'Save As'
      C. No buttons because 'Save' is part of 'Save As'
      D. The first button with text 'Save' only

      Solution

      1. Step 1: Understand exact text() matching

        The XPath text()='Save' matches only buttons whose visible text is exactly 'Save'.
      2. Step 2: Analyze the buttons' text

        Only the first button has exactly 'Save'. The third button's text is 'Save As', which does not match exactly.
      3. Final Answer:

        The first button with text 'Save' only -> Option D
      4. Quick Check:

        text()='Save' matches exact text only [OK]
      Hint: Exact text() matches full text, not partial [OK]
      Common Mistakes:
      • Assuming partial match with text()
      • Selecting multiple elements incorrectly
      • Ignoring spaces or extra words
      4. You wrote this XPath to select a label with text 'Username': //label[text()='username']. It does not find the element. What is the likely problem?
      medium
      A. Missing contains() function for partial match
      B. XPath is case-sensitive; 'username' does not match 'Username'
      C. text() cannot be used with label elements
      D. XPath syntax requires @text attribute instead of text()

      Solution

      1. Step 1: Check case sensitivity in XPath text()

        XPath text() matching is case-sensitive, so 'username' does not match 'Username'.
      2. Step 2: Verify other options

        text() works with any element, contains() is optional, and @text is invalid attribute syntax.
      3. Final Answer:

        XPath is case-sensitive; 'username' does not match 'Username' -> Option B
      4. Quick Check:

        XPath text() is case-sensitive [OK]
      Hint: Match exact case in text() for correct XPath [OK]
      Common Mistakes:
      • Ignoring case sensitivity
      • Using attribute syntax instead of text()
      • Assuming text() only works on certain tags
      5. You want to select a <span> element that contains the exact text 'Error: Invalid input' but the page sometimes adds extra spaces before or after the text. Which XPath expression is best to handle this?
      hard
      A. //span[text()='Error: Invalid input']
      B. //span[contains(text(), 'Error: Invalid input')]
      C. //span[normalize-space(text())='Error: Invalid input']
      D. //span[@text='Error: Invalid input']

      Solution

      1. Step 1: Understand the problem with extra spaces

        Extra spaces before or after text cause exact text() match to fail.
      2. Step 2: Use normalize-space() to trim spaces

        Using normalize-space(text())='Error: Invalid input' trims spaces and matches exact text.
      3. Step 3: Evaluate other options

        //span[text()='Error: Invalid input'] fails with spaces, //span[contains(text(), 'Error: Invalid input')] matches partial text (not exact), //span[@text='Error: Invalid input'] is invalid syntax.
      4. Final Answer:

        //span[normalize-space(text())='Error: Invalid input'] -> Option C
      5. Quick Check:

        normalize-space() trims spaces for exact text match [OK]
      Hint: Use normalize-space() to ignore extra spaces in text() [OK]
      Common Mistakes:
      • Using text() without trimming spaces
      • Using contains() when exact match needed
      • Treating text as attribute
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard