Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find an element whose text contains 'Submit'.
Selenium Python
element = driver.find_element(By.XPATH, "//*[contains(text(), '[1]')]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the text inside contains()
Using incorrect case in the text string
✗ Incorrect
The contains() function in XPath checks if the text contains the exact string 'Submit'.
2fill in blank
mediumComplete the code to find an element whose attribute 'id' starts with 'user_'.
Selenium Python
element = driver.find_element(By.XPATH, "//*[starts-with(@id, '[1]')]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the attribute name as the string to match
Omitting the quotes around the prefix string
✗ Incorrect
The starts-with() function checks if the attribute 'id' begins with 'user_'.
3fill in blank
hardFix the error in the XPath to find a button with class containing 'submit-btn'.
Selenium Python
element = driver.find_element(By.XPATH, "//button[contains(@class, '[1]')]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the string inside contains()
Using underscores instead of hyphens
✗ Incorrect
The string inside contains() must be quoted properly inside the XPath string.
4fill in blank
hardFill both blanks to find a div whose id starts with 'main' and contains 'content'.
Selenium Python
element = driver.find_element(By.XPATH, "//div[starts-with(@id, '[1]') and contains(@id, '[2]')]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of starts-with and contains
Forgetting quotes around strings
✗ Incorrect
The XPath uses starts-with() for 'main' and contains() for 'content' on the id attribute.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps element text to id for elements whose class starts with 'btn' and contains 'active'.
Selenium Python
result = {element.text: element.get_attribute('[1]') for element in elements if element.get_attribute('[2]').[3]('btn') and 'active' in element.get_attribute('class')} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'id' for dictionary values
Using contains() method incorrectly in Python
Not calling startswith() as a method
✗ Incorrect
We get the 'id' attribute for the dictionary value, check 'class' attribute for startswith('btn'), and use the startswith() method.