Remember that XPath text() matches exact text content including case and spaces.
✗ Incorrect
The XPath //button[text()='Submit'] matches only buttons whose text content is exactly 'Submit'. The first button matches exactly. The second has lowercase 'submit' and the third has spaces, so they do not match.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies the presence of a button with text 'Cancel' using XPath?
You want to assert that a button with text 'Cancel' exists on the page using Selenium and XPath. Which assertion is correct?
Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
XPath text() is case-sensitive and matches exact text.
✗ Incorrect
Option B uses the exact text 'Cancel' which matches the button text exactly. Option B is missing a closing bracket and will cause a syntax error. Option B uses lowercase 'cancel' which won't match if the text is 'Cancel'. Option B includes spaces which must match exactly, so it won't match if the button text has no spaces.
❓ locator
advanced
2:00remaining
Which XPath locator correctly selects a
containing exact text 'Error 404'?
Choose the XPath expression that selects a
element whose text content is exactly 'Error 404'.
A//div[text()='Error 404']
B//div[contains(text(),'Error 404')]
C//div[text()=' error 404']
D//div[text()='Error404']
Attempts:
2 left
💡 Hint
Exact text matching requires exact string including case and spaces.
✗ Incorrect
Option A matches exactly 'Error 404'. Option A matches any div containing 'Error 404' but not necessarily exact text. Options A and C have incorrect text strings and will not match.
🔧 Debug
advanced
2:00remaining
Why does this XPath fail to find the element?
Given the XPath //span[text()='Login'] does not find the element, but the element's visible text is 'Login'. What is the likely cause?
Selenium Python
<span> Login </span>
AThe XPath fails because <span> elements cannot be selected by text()
BThe XPath fails because 'Login' is case-insensitive in XPath
CThe XPath fails because the text has leading/trailing spaces not matched by text()
DThe XPath fails because the element is hidden
Attempts:
2 left
💡 Hint
Check for spaces around the text inside the element.
✗ Incorrect
XPath text() matches exact text including spaces. If the element text is ' Login ' with spaces, text()='Login' will not match.
❓ framework
expert
3:00remaining
Which Selenium Python code snippet correctly waits for a button with exact text 'Submit' to be clickable?
Select the correct code snippet that waits up to 15 seconds for a button with text 'Submit' to be clickable using Selenium WebDriverWait and XPath.
Option D uses correct XPath with exact text and correct syntax. Option D misses a closing bracket causing syntax error. Option D uses wrong case 'submit'. Option D includes spaces that may not match the actual button text.
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
Step 1: Understand the XPath syntax
The expression //button selects all button elements in the page.
Step 2: Analyze the text() condition
The predicate [text()='Submit'] filters buttons whose visible text exactly matches 'Submit'.
Final Answer:
A button element with the exact visible text 'Submit' -> Option C
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
Step 1: Check the correct use of text() function
The correct syntax to match exact text is text()='Home' inside square brackets.
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'.
Final Answer:
//a[text()='Home'] -> Option B
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
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
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
The find_elements method returns a list of matching elements. Its length is 2.
Final Answer:
2 -> Option A
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
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
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.
Final Answer:
The button text has extra spaces or different case -> Option A
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
Step 1: Understand the problem with spaces
Text may have extra spaces, so exact text()='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'] uses normalize-space() without text(), which may fail if element has multiple text nodes.
Final Answer:
//div[normalize-space(text())='Error: Invalid input'] -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.