0
0
Selenium Javatesting~10 mins

Absolute vs relative XPath in Selenium Java - Interactive 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 using an absolute XPath.

Selenium Java
WebElement element = driver.findElement(By.xpath("[1]"));
Drag options to blanks, or click blank then click option'
A"/html/body/div[1]/div[2]/button"
B"//button[@id='submit']"
C".//div[@class='container']"
D"//*[@class='btn']"
Attempts:
3 left
💡 Hint
Common Mistakes
Using relative XPath when absolute is required.
Starting XPath with '//' for absolute path.
2fill in blank
medium

Complete the code to find an element using a relative XPath.

Selenium Java
WebElement element = driver.findElement(By.xpath("[1]"));
Drag options to blanks, or click blank then click option'
A"//button[@id='submit']"
B"/html/body//button"
C"/html/body/div[1]/div[2]/button"
D"/html//div/button"
Attempts:
3 left
💡 Hint
Common Mistakes
Using absolute XPath when relative is better.
Missing the double slashes at the start.
3fill in blank
hard

Fix the error in the XPath to correctly select the button with id 'login'.

Selenium Java
WebElement loginButton = driver.findElement(By.xpath("[1]"));
Drag options to blanks, or click blank then click option'
A"//button[id='login']"
B"/button[@id='login']"
C"button[@id='login']"
D"//button[@id='login']"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single slash '/' which looks only at direct children.
Missing '@' before attribute name.
Using 'id' without '@'.
4fill in blank
hard

Fill both blanks to create a relative XPath that selects all div elements with class 'content'.

Selenium Java
List<WebElement> divs = driver.findElements(By.xpath("[1][@class=[2]]"));
Drag options to blanks, or click blank then click option'
A"//div"
B"content"
C"'content'"
D"div"
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the attribute value.
Using single slash '/' instead of '//'.
5fill in blank
hard

Fill all three blanks to create an XPath that selects input elements with type 'text' inside a form with id 'searchForm'.

Selenium Java
WebElement input = driver.findElement(By.xpath("[1][@id=[2]]//[3][@type='text']"));
Drag options to blanks, or click blank then click option'
A"//form"
B"'searchForm'"
C"input"
D"form"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single slash '/' instead of '//' for relative paths.
Not quoting attribute values.
Using wrong element names.