Bird
Raised Fist0
Selenium Pythontesting~5 mins

XPath with text() in Selenium Python - 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 inside an element. It helps locate elements by matching their visible text.
Click to reveal answer
beginner
Write an XPath to find a button with the exact text 'Submit'.
Use //button[text()='Submit']. This finds a button element whose text is exactly 'Submit'.
Click to reveal answer
intermediate
How do you find an element containing text 'Login' anywhere inside it using XPath?
Use //*[contains(text(), 'Login')]. This finds any element where the text includes 'Login' anywhere inside.
Click to reveal answer
beginner
Why is using text() in XPath useful for testing?
It helps find elements by their visible text, which is often stable and user-friendly. This makes tests easier to read and maintain.
Click to reveal answer
intermediate
What is a limitation of using text() in XPath?
If the element has nested tags or extra spaces, text() might not match exactly. You may need to use normalize-space() or other functions.
Click to reveal answer
Which XPath expression finds a div with exact text 'Hello World'?
A//div[@text='Hello World']
B//div[contains(text(),'Hello World')]
C//div[text()='Hello World']
D//div[text()*='Hello World']
What does //*[contains(text(),'Click')] select?
AAny element containing the word 'Click' anywhere in its text
BElements with attribute 'text' containing 'Click'
COnly <code>button</code> elements with text 'Click'
DAny element with text exactly 'Click'
Why might text() fail to match an element's visible text?
ABecause <code>text()</code> only matches attributes
BBecause the element has nested tags or extra spaces
CBecause <code>text()</code> is case-insensitive
DBecause <code>text()</code> matches only numbers
Which function helps remove extra spaces when using text() in XPath?
Astrip()
Btrim()
Cclean-text()
Dnormalize-space()
In Selenium Python, how do you find an element with text 'Home' using XPath?
Adriver.find_element(By.XPATH, "//*[text()='Home']")
Bdriver.find_element(By.ID, "Home")
Cdriver.find_element(By.XPATH, "//*[contains(text(),'Home')]", "")
Ddriver.find_element(By.CLASS_NAME, "Home")
Explain how the XPath text() function helps in locating elements during testing.
Think about how you find buttons or links by their labels.
You got /3 concepts.
    Describe a situation where using text() in XPath might not work as expected and how to fix it.
    Consider complex HTML structures or formatting.
    You got /3 concepts.

      Practice

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

      Solution

      1. Step 1: Understand the XPath syntax

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

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

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

        XPath with text() matches exact visible text [OK]
      Hint: Use text() to match exact visible text in XPath [OK]
      Common Mistakes:
      • Confusing attribute matching with text()
      • Assuming partial text matches work
      • Ignoring case sensitivity in text()
      2. Which of the following is the correct XPath syntax to select a link (<a>) with the visible text exactly 'Home'?
      easy
      A. //a[contains(text(), 'Home')]
      B. //a[text()='Home']
      C. //a[@text='Home']
      D. //a[text() = Home]

      Solution

      1. Step 1: Check the correct use of text() function

        The correct syntax to match exact text is text()='Home' inside square brackets.
      2. Step 2: Identify syntax errors in other options

        //a[contains(text(), 'Home')] uses contains() which matches partial text, not exact. //a[@text='Home'] incorrectly treats text as an attribute. //a[text() = Home] misses quotes around 'Home'.
      3. Final Answer:

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

        Exact text match requires text()='value' with quotes [OK]
      Hint: Exact text match uses text()='value' with quotes [OK]
      Common Mistakes:
      • Using contains() instead of text() for exact match
      • Treating text as an attribute
      • Missing quotes around text value
      3. Given the following HTML snippet:
      <div>
        <span>Login</span>
        <span>Logout</span>
        <span>Login</span>
      </div>

      What will be the result of this Selenium Python code?
      elements = driver.find_elements(By.XPATH, "//span[text()='Login']")
      print(len(elements))
      medium
      A. 2
      B. 1
      C. 3
      D. 0

      Solution

      1. Step 1: Identify elements matching XPath

        The XPath //span[text()='Login'] selects all <span> elements with exact text 'Login'. There are two such spans in the HTML.
      2. Step 2: Count the matched elements

        The find_elements method returns a list of matching elements. Its length is 2.
      3. Final Answer:

        2 -> Option A
      4. Quick Check:

        Count of spans with text 'Login' = 2 [OK]
      Hint: Count elements with exact text using find_elements and len() [OK]
      Common Mistakes:
      • Assuming only one element matches
      • Confusing find_element with find_elements
      • Ignoring exact text match requirement
      4. You wrote this XPath to find a button with text 'Cancel': //button[text()='Cancel']. But your test fails to find the button. What is the most likely reason?
      medium
      A. The button text has extra spaces or different case
      B. The XPath syntax is invalid
      C. The button element does not exist on the page
      D. The text() function matches partial text only

      Solution

      1. Step 1: Check XPath syntax correctness

        The XPath //button[text()='Cancel'] is syntactically correct, so syntax error is unlikely.
      2. Step 2: Consider text matching behavior

        The text() function matches exact visible text including spaces and case. If the button text has extra spaces or different case (e.g., ' cancel ' or 'CANCEL'), it won't match.
      3. Final Answer:

        The button text has extra spaces or different case -> Option A
      4. Quick Check:

        Exact text match fails if spaces or case differ [OK]
      Hint: Check exact text including spaces and case for text() [OK]
      Common Mistakes:
      • Assuming text() matches partial or case-insensitive text
      • Ignoring leading/trailing spaces in text
      • Blaming XPath syntax without checking text content
      5. You want to select a <div> element that contains exactly the text 'Error: Invalid input' but the text may have leading or trailing spaces. Which XPath expression correctly handles this using text() and normalize-space()?
      hard
      A. //div[normalize-space()='Error: Invalid input']
      B. //div[text()='Error: Invalid input']
      C. //div[contains(text(), 'Error: Invalid input')]
      D. //div[normalize-space(text())='Error: Invalid input']

      Solution

      1. Step 1: Understand the problem with spaces

        Text may have extra spaces, so exact text()='Error: Invalid input' won't match if spaces differ.
      2. Step 2: Use normalize-space() to trim spaces

        normalize-space(text()) removes leading and trailing spaces before comparison, ensuring correct match.
      3. Step 3: Evaluate options

        //div[text()='Error: Invalid input'] fails if spaces differ. //div[contains(text(), 'Error: Invalid input')] matches partial text, not exact. //div[normalize-space()='Error: Invalid input'] uses normalize-space() without text(), which may fail if element has multiple text nodes.
      4. Final Answer:

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

        Use normalize-space(text()) for exact text ignoring spaces [OK]
      Hint: Use normalize-space(text()) to ignore extra spaces in XPath [OK]
      Common Mistakes:
      • Not trimming spaces before text comparison
      • Using contains() for exact match
      • Applying normalize-space() incorrectly
      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