0
0
Selenium Pythontesting~20 mins

Absolute vs relative XPath in Selenium Python - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XPath Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Absolute XPath
Which statement best describes an absolute XPath in Selenium?
AIt uses CSS selectors to locate elements.
BIt starts with '//' and searches anywhere in the document.
CIt starts from the root node and specifies the full path to the element.
DIt only works with elements having unique IDs.
Attempts:
2 left
💡 Hint
Think about how the path starts from the very top of the HTML document.
Predict Output
intermediate
1:30remaining
Output of XPath Locator in Selenium
Given the following HTML snippet and XPath, what element text will Selenium find?
Selenium Python
<html><body><div><p>First</p><p>Second</p></div></body></html>

xpath = '/html/body/div/p[2]'
A"First"
B"Second"
CNoSuchElementException
D"div"
Attempts:
2 left
💡 Hint
XPath index starts at 1, so p[2] means the second

element inside

.
locator
advanced
2:00remaining
Choosing the Correct Relative XPath
Which XPath expression correctly selects all elements with a class attribute containing 'search' anywhere in the document?
A/input[contains(@class, 'search')]
B/html/body//input[contains(@class, 'search')]
Cinput[contains(@class, 'search')]
D//input[contains(@class, 'search')]
Attempts:
2 left
💡 Hint
Relative XPath starts with '//' to search anywhere in the document.
assertion
advanced
1:30remaining
Validating Element Presence Using XPath
Which assertion correctly verifies that an element located by relative XPath exists on the page using Selenium in Python?
Selenium Python
element = driver.find_element(By.XPATH, "//button[@id='submit']")
Aassert element is not None
Bassert element.exists()
Cassert driver.find_element(By.XPATH, "//button[@id='submit']") is True
Dassert driver.find_element(By.XPATH, "//button[@id='submit']") == True
Attempts:
2 left
💡 Hint
find_element throws an exception if not found, so checking for None is safe after catching exceptions.
🔧 Debug
expert
2:30remaining
Debugging a Failing XPath Locator
You have this XPath locator in your Selenium test: '/html/body/div[2]/span/input'. The test fails with NoSuchElementException. Which is the most likely reason?
AThe page structure changed, so the absolute XPath no longer matches the element.
BRelative XPath should be used instead of absolute XPath for all elements.
CThe input element does not have an ID attribute.
DThe XPath syntax is invalid because it uses numbers in brackets.
Attempts:
2 left
💡 Hint
Absolute XPath is fragile and breaks if page layout changes.