0
0
Selenium Pythontesting~10 mins

XPath with contains and starts-with in Selenium Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"button"
B"submit"
C"Submit"
D"Click"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the text inside contains()
Using incorrect case in the text string
2fill in blank
medium

Complete 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'
A"id"
B"name_"
C"btn_"
D"user_"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the attribute name as the string to match
Omitting the quotes around the prefix string
3fill in blank
hard

Fix 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'
A"submit-btn"
Bsubmit-btn
Csubmit_btn
D'submit-btn'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the string inside contains()
Using underscores instead of hyphens
4fill in blank
hard

Fill 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'
A"main"
B"content"
C"container"
D"header"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of starts-with and contains
Forgetting quotes around strings
5fill in blank
hard

Fill 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'
Aid
Bclass
Cstartswith
Dget_attribute
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