0
0
Selenium Javatesting~5 mins

Absolute vs relative XPath in Selenium Java - Quick Revision & Key Differences

Choose your learning style9 modes available
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?
ARelative XPath
BAbsolute XPath
CCSS Selector
DID Selector
Which XPath is more flexible and less likely to break if the page layout changes?
ARelative XPath
BTag Name Selector
CAbsolute XPath
DClass Name Selector
What does a relative XPath start with?
A/
B.
C//
D#
Which XPath would be fragile if a new div is added at the top of the page?
AAbsolute XPath
BRelative XPath
CCSS Selector
DName Selector
Which XPath is best to locate a button with specific text?
A/html/body/div/button
B//*[@class='button']
C//div[@id='button']
D//button[text()='Submit']
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.