XPath with text() helps find web elements by matching their visible text. This makes locating elements easier when you know the exact text shown on the page.
XPath with text() in Selenium Python
//tagname[text()='exact text']Replace tagname with the HTML element name like button, a, or div.
The text inside text()='...' must match exactly, including spaces and case.
button element whose visible text is exactly 'Submit'.//button[text()='Submit']a) with the text 'Home'.//a[text()='Home']//p[text()='Welcome to our site!']This script opens a simple HTML page with buttons, a link, and a paragraph. It uses XPath with text() to find elements by their visible text and prints their text content.
from selenium import webdriver from selenium.webdriver.common.by import By # Setup WebDriver (make sure driver executable is in PATH) driver = webdriver.Chrome() # Open a simple test page html = ''' <html> <body> <button>Submit</button> <button>Cancel</button> <a href='#'>Home</a> <p>Welcome to our site!</p> </body> </html> ''' # Load the HTML content using data URL driver.get('data:text/html;charset=utf-8,' + html) # Find the Submit button using XPath with text() submit_button = driver.find_element(By.XPATH, "//button[text()='Submit']") print('Submit button text:', submit_button.text) # Find the Home link home_link = driver.find_element(By.XPATH, "//a[text()='Home']") print('Home link text:', home_link.text) # Find the paragraph welcome_para = driver.find_element(By.XPATH, "//p[text()='Welcome to our site!']") print('Paragraph text:', welcome_para.text) # Close the browser driver.quit()
Text matching with text() is exact. Use functions like contains() if partial matching is needed.
Whitespace and case must match exactly when using text().
Using text() is helpful when element attributes are not reliable or missing.
XPath with text() finds elements by their exact visible text.
It is useful for buttons, links, and messages where text is stable.
Remember to match text exactly, including spaces and case.
Practice
//button[text()='Submit'] select in a web page?Solution
Step 1: Understand the XPath syntax
The expression//buttonselects all button elements in the page.Step 2: Analyze the
The predicatetext()condition[text()='Submit']filters buttons whose visible text exactly matches 'Submit'.Final Answer:
A button element with the exact visible text 'Submit' -> Option CQuick Check:
XPath with text() matches exact visible text [OK]
- Confusing attribute matching with text()
- Assuming partial text matches work
- Ignoring case sensitivity in text()
<a>) with the visible text exactly 'Home'?Solution
Step 1: Check the correct use of text() function
The correct syntax to match exact text istext()='Home'inside square brackets.Step 2: Identify syntax errors in other options
//a[contains(text(), 'Home')] usescontains()which matches partial text, not exact. //a[@text='Home'] incorrectly treats text as an attribute. //a[text() = Home] misses quotes around 'Home'.Final Answer:
//a[text()='Home'] -> Option BQuick Check:
Exact text match requires text()='value' with quotes [OK]
- Using contains() instead of text() for exact match
- Treating text as an attribute
- Missing quotes around text value
<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))
Solution
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.Step 2: Count the matched elements
Thefind_elementsmethod returns a list of matching elements. Its length is 2.Final Answer:
2 -> Option AQuick Check:
Count of spans with text 'Login' = 2 [OK]
- Assuming only one element matches
- Confusing find_element with find_elements
- Ignoring exact text match requirement
//button[text()='Cancel']. But your test fails to find the button. What is the most likely reason?Solution
Step 1: Check XPath syntax correctness
The XPath//button[text()='Cancel']is syntactically correct, so syntax error is unlikely.Step 2: Consider text matching behavior
Thetext()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.Final Answer:
The button text has extra spaces or different case -> Option AQuick Check:
Exact text match fails if spaces or case differ [OK]
- Assuming text() matches partial or case-insensitive text
- Ignoring leading/trailing spaces in text
- Blaming XPath syntax without checking text content
<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()?Solution
Step 1: Understand the problem with spaces
Text may have extra spaces, so exacttext()='Error: Invalid input'won't match if spaces differ.Step 2: Use normalize-space() to trim spaces
normalize-space(text())removes leading and trailing spaces before comparison, ensuring correct match.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'] usesnormalize-space()withouttext(), which may fail if element has multiple text nodes.Final Answer:
//div[normalize-space(text())='Error: Invalid input'] -> Option DQuick Check:
Use normalize-space(text()) for exact text ignoring spaces [OK]
- Not trimming spaces before text comparison
- Using contains() for exact match
- Applying normalize-space() incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
