0
0
Selenium Javatesting~10 mins

XPath axes (parent, following-sibling, preceding) in Selenium Java - Interactive Code Practice

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

Complete the code to locate the parent element of a given child element using XPath.

Selenium Java
WebElement parent = driver.findElement(By.xpath("//div[@id='child'][1]"));
Drag options to blanks, or click blank then click option'
A/parent::*
B/parent::div
C/ancestor::div
D/child::div
Attempts:
3 left
💡 Hint
Common Mistakes
Using child axis instead of parent axis
Using ancestor axis which selects all ancestors, not just the parent
2fill in blank
medium

Complete the code to find the following sibling element of a specific element using XPath.

Selenium Java
WebElement sibling = driver.findElement(By.xpath("//li[@class='item'][1]"));
Drag options to blanks, or click blank then click option'
A/preceding-sibling::li[1]
B/following-sibling::li[1]
C/child::li[1]
D/parent::li
Attempts:
3 left
💡 Hint
Common Mistakes
Using preceding-sibling instead of following-sibling
Using child axis which selects children, not siblings
3fill in blank
hard

Fix the error in the XPath to select the preceding sibling div element.

Selenium Java
WebElement prevSibling = driver.findElement(By.xpath("//span[@id='current'][1]"));
Drag options to blanks, or click blank then click option'
A/ancestor::div
B/following-sibling::div[1]
C/preceding-sibling::div[1]
D/parent::div
Attempts:
3 left
💡 Hint
Common Mistakes
Using following-sibling instead of preceding-sibling
Using parent or ancestor axis which select ancestors, not siblings
4fill in blank
hard

Fill both blanks to select the parent div and then its following sibling span element.

Selenium Java
WebElement element = driver.findElement(By.xpath("//p[1][2]"));
Drag options to blanks, or click blank then click option'
A/parent::div
B/following-sibling::span
C/preceding-sibling::span
D/child::span
Attempts:
3 left
💡 Hint
Common Mistakes
Using preceding-sibling instead of following-sibling
Using child axis which selects children, not siblings
5fill in blank
hard

Fill all three blanks to select a div element that is the parent of a span, and then select its preceding sibling li element.

Selenium Java
WebElement element = driver.findElement(By.xpath("//span[1][2][3]"));
Drag options to blanks, or click blank then click option'
A/parent::div
B/preceding-sibling::li
C/following-sibling::li
D/child::li
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up following-sibling and preceding-sibling
Using child axis too early