Recall & Review
beginner
What is an absolute XPath?
An absolute XPath is a full path from the root element to the target element in the HTML document. It starts with a single slash (/). It shows the exact location of the element in the page structure.
Click to reveal answer
beginner
What is a relative XPath?
A relative XPath starts from anywhere in the document and finds the element based on conditions or attributes. It starts with double slashes (//). It is shorter and more flexible than absolute XPath.
Click to reveal answer
intermediate
Why is relative XPath preferred over absolute XPath in Selenium tests?
Relative XPath is preferred because it is less likely to break if the page layout changes. Absolute XPath depends on the full path, so small changes in the page can cause failures.
Click to reveal answer
beginner
Example of an absolute XPath for a button element:
/html/body/div[2]/div[1]/button[1] <br> This means starting from the root html, go to body, then second div, then first div inside it, then first button.
Click to reveal answer
beginner
Example of a relative XPath for a button with text 'Submit':
//button[text()='Submit'] <br> This finds any button element with the exact text 'Submit' anywhere in the page.
Click to reveal answer
Which XPath starts with a single slash '/'?
✗ Incorrect
Absolute XPath always starts with a single slash '/' indicating the path from the root element.
Which XPath is more flexible and less likely to break when page layout changes?
✗ Incorrect
Relative XPath uses conditions and attributes, so it adapts better to changes in page structure.
What does the XPath '//input[@id="username"]' represent?
✗ Incorrect
Double slashes '//' indicate relative XPath searching anywhere in the document for input with id 'username'.
Which XPath is easier to maintain in automated tests?
✗ Incorrect
Relative XPath is easier to maintain because it is less sensitive to changes in page layout.
What is a risk of using absolute XPath in tests?
✗ Incorrect
Absolute XPath depends on the full path, so small changes in the page can cause the locator to fail.
Explain the difference between absolute and relative XPath with examples.
Think about how each XPath starts and how they locate elements.
You got /5 concepts.
Describe why using relative XPath is a better practice in Selenium test automation.
Consider what happens when the page layout changes.
You got /4 concepts.