0
0
Selenium Pythontesting~10 mins

Find element by XPath 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 by XPath using Selenium.

Selenium Python
element = driver.find_element_by_[1]("//button[@id='submit']")
Drag options to blanks, or click blank then click option'
Axpath
Bid
Ccss_selector
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' or 'name' instead of 'xpath' when the locator is an XPath expression.
Misspelling the method name.
2fill in blank
medium

Complete the code to find an element by XPath that contains specific text.

Selenium Python
element = driver.find_element_by_xpath("//div[contains(text(), '[1]')]")
Drag options to blanks, or click blank then click option'
ACancel
BClick me
CSubmit
DHome
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect text that does not exist in the element.
Forgetting to put the text inside quotes.
3fill in blank
hard

Fix the error in the code to correctly find an element by XPath.

Selenium Python
element = driver.find_element_by_xpath("//input[@type='[1]']")
Drag options to blanks, or click blank then click option'
Asubmit
Bbutton
Ccheckbox
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'button' instead of 'submit' for the type attribute.
Using 'text' which selects text input fields, not submit buttons.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps element texts to their XPath using Selenium.

Selenium Python
elements = driver.find_elements_by_xpath("//a")
links = {el.text: el.get_attribute([1]) for el in elements if el.text [2] ''}
Drag options to blanks, or click blank then click option'
A"href"
B!=
C==
D"src"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'src' attribute which is for images, not links.
Using '==' instead of '!=' which would select empty text elements.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase element texts to their href attribute if href contains 'example'.

Selenium Python
elements = driver.find_elements_by_xpath("//a")
filtered_links = {el.text[1](): el.get_attribute([2]) for el in elements if el.get_attribute([3]) and 'example' in el.get_attribute('href')}
Drag options to blanks, or click blank then click option'
A.upper
B"href"
D.lower
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.lower()' instead of '.upper()' when uppercase is required.
Checking a wrong attribute name in the condition.