0
0
Selenium Pythontesting~10 mins

XPath axes (parent, child, sibling) 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 the parent element using XPath axes.

Selenium Python
parent_element = driver.find_element(By.XPATH, "//div[@id='child'][1]")
Drag options to blanks, or click blank then click option'
A/preceding-sibling::div
B/parent::div
C/following-sibling::div
D/child::div
Attempts:
3 left
💡 Hint
Common Mistakes
Using child axis instead of parent axis.
Using sibling axes when parent is needed.
2fill in blank
medium

Complete the code to find all child <li> elements of a <ul> using XPath axes.

Selenium Python
child_items = driver.find_elements(By.XPATH, "//ul[1]")
Drag options to blanks, or click blank then click option'
A/ancestor::li
B/parent::li
C/following-sibling::li
D/child::li
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent axis instead of child axis.
Using sibling axes which select siblings, not children.
3fill in blank
hard

Fix the error in the XPath to select the next sibling <div> element.

Selenium Python
next_sibling = driver.find_element(By.XPATH, "//div[@class='current'][1]")
Drag options to blanks, or click blank then click option'
A/following-sibling::div[1]
B/child::div
C/parent::div
D/preceding-sibling::div
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent or child axes instead of sibling axes.
Not specifying the index to get the immediate sibling.
4fill in blank
hard

Fill both blanks to select all sibling <span> elements before and after the current <div>.

Selenium Python
siblings = driver.find_elements(By.XPATH, "//div[@id='main'][1] | //div[@id='main'][2]")
Drag options to blanks, or click blank then click option'
A/preceding-sibling::span
B/child::span
C/following-sibling::span
D/parent::span
Attempts:
3 left
💡 Hint
Common Mistakes
Using child or parent axes instead of sibling axes.
Not combining the two XPath expressions correctly.
5fill in blank
hard

Fill all three blanks to create a dictionary mapping each <li> text to its parent's id and the next sibling's text.

Selenium Python
result = {item.text: {'parent_id': item.find_element(By.XPATH, '[1]').get_attribute('id'), 'next_sibling_text': item.find_element(By.XPATH, '[2]').text} for item in driver.find_elements(By.XPATH, '//li') if item.find_elements(By.XPATH, '[3]')}
Drag options to blanks, or click blank then click option'
A/parent::*
B/following-sibling::*[1]
C/child::*
D/preceding-sibling::*
Attempts:
3 left
💡 Hint
Common Mistakes
Using child axis instead of parent for getting parent id.
Not specifying the immediate next sibling with [1].
Using preceding-sibling instead of following-sibling for next sibling.