Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find an element by exact text using XPath.
Selenium Python
element = driver.find_element(By.XPATH, "//button[text()=[1]]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes inside double quotes without escaping causes syntax errors.
Using incorrect case in text() causes no match.
✗ Incorrect
The XPath text() function requires the exact text in single quotes inside the XPath string. Using single quotes inside double quotes is correct.
2fill in blank
mediumComplete the code to find a link element containing partial text using XPath.
Selenium Python
element = driver.find_element(By.XPATH, "//a[contains(text(), [1])]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the text inside contains() causes syntax errors.
Using wrong case in text causes no match.
✗ Incorrect
The contains() function in XPath matches partial text. The text must be in quotes inside the XPath string. Double quotes inside double quotes require escaping or using single quotes.
3fill in blank
hardFix the error in the XPath expression to find a div with exact text 'Welcome'.
Selenium Python
element = driver.find_element(By.XPATH, "//div[text()=[1]]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the text inside text() causes XPath syntax errors.
Using wrong case in text causes no match.
✗ Incorrect
The text() function requires the text to be quoted as a string literal. Using unquoted text causes XPath syntax error.
4fill in blank
hardFill both blanks to find a span element with text exactly 'Hello' and class 'greeting'.
Selenium Python
element = driver.find_element(By.XPATH, "//span[text()=[1] and @class=[2]]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting text() or attribute values causes XPath errors.
Mixing quote types incorrectly causes syntax errors.
✗ Incorrect
The text() function and attribute values must be quoted strings inside the XPath expression. Single quotes inside double quotes are common and consistent here.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps button text to their ids for buttons containing 'Click'.
Selenium Python
buttons = driver.find_elements(By.XPATH, "//button[contains(text(), [1])]") result = [2]: [3] for btn in buttons}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using btn.text() instead of btn.text causes attribute errors.
Not quoting the text in XPath causes syntax errors.
Swapping key and value in dictionary comprehension.
✗ Incorrect
The XPath contains() function needs the partial text quoted. In the dictionary comprehension, the key is the button text accessed by btn.text, and the value is the button's id attribute accessed by btn.get_attribute('id'). btn.text() is invalid in Selenium Python.