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 elements 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 fragile. If the page layout changes, absolute XPath may break since it depends on the full path. Relative XPath uses attributes or conditions, so it adapts better to changes.
Click to reveal answer
beginner
Example of an absolute XPath for a button element:
/html/body/div[2]/div[1]/button
Click to reveal answer
beginner
Example of a relative XPath for a button with text 'Submit':
//button[text()='Submit']
Click to reveal answer
Which XPath starts from the root element and shows the full path?
✗ Incorrect
Absolute XPath starts with a single slash (/) and shows the full path from the root.
Which XPath is more flexible and less likely to break if the page layout changes?
✗ Incorrect
Relative XPath uses conditions and attributes, making it more adaptable to page changes.
What does a relative XPath start with?
✗ Incorrect
Relative XPath starts with double slashes (//) to search anywhere in the document.
Which XPath would be fragile if a new div is added at the top of the page?
✗ Incorrect
Absolute XPath depends on the full path, so adding elements can break it.
Which XPath is best to locate a button with specific text?
✗ Incorrect
Using relative XPath with text() function targets the button by its visible text.
Explain the difference between absolute and relative XPath and when to use each.
Think about how page changes affect each XPath type.
You got /5 concepts.
Write an example of an absolute XPath and a relative XPath for a login button.
Use /html/body/... for absolute and //button[@id='login'] or //button[text()='Login'] for relative.
You got /3 concepts.