0
0
Selenium Pythontesting~5 mins

Absolute vs relative XPath in Selenium Python - 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 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 '/'?
ANeither
BAbsolute XPath
CBoth absolute and relative
DRelative XPath
Which XPath is more flexible and less likely to break when page layout changes?
AAbsolute XPath
BBoth are equally flexible
CRelative XPath
DNone of the above
What does the XPath '//input[@id="username"]' represent?
AInvalid XPath
BAbsolute path to input element with id 'username'
CRelative path to root element
DRelative path to any input element with id 'username'
Which XPath is easier to maintain in automated tests?
ARelative XPath
BAbsolute XPath
CBoth are equally easy
DDepends on the browser
What is a risk of using absolute XPath in tests?
AIt breaks easily if page structure changes
BIt cannot locate elements
CIt runs slower
DIt requires special browser settings
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.