- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between relative and absolute XPath in Selenium?
We can have two ways of creating an xpath – relative and absolute. The absolute xpath has the complete path beginning from the root to the element which we want to identify.
An absolute xpath starts with the / symbol. One drawback with the absolute xpath is that if there is any change in attributes beginning from the root to the element, our absolute xpath will become invalid.
The relative xpath starts by referring to the element that we want to identify and not from the root node. A relative xpath starts with the // symbol. It is mainly used for automation since even if an element is removed or added in the DOM, the relative xpath is not impacted.
An absolute xpath is lengthy and difficult to maintain (html/body/tagname/…). While a relative xpath is short (//*[@attribute='value']). Let us identify the Home menu on the below page −
Let us investigate the html code of the Home element starting from the root.
The absolute xpath for this element is /html/body/div/div/div/a. It can be verified with the expression $x("/html/body/div/div/div/a") in the browser Console(opens by pressing F12).The image shows the matching element obtained from the absolute xpath expression.
The relative xpath for this element is //a[@title='TutorialsPoint - Home']. It can be verified with the expression $x("//a[@title='TutorialsPoint - Home']") in the browser Console. The image shows the matching element obtained from the relative xpath expression.
- What is the primary difference between the XPath and CSS selector in Selenium?
- What is xpath in Selenium?
- What is the difference between selenium and automation?
- What is the difference between mocha and Selenium?
- What is difference between selenium 2 and selenium 3?
- What is difference between selenium 1 and Selenium 2
- What are the differences between xpath and css in Selenium with python?
- What is xpath in Selenium with python?
- What is the difference between selenium WebDriver and TestNG?
- What is the difference between Selenium RC and Webdriver?
- CSS Absolute and Relative Units
- What is the difference between getWindowHandle() and getWindowHandles() in Selenium?
- What is the difference between Assert and Verify in Selenium?
- Absolute and Relative frequency in Pandas
- Absolute and Relative Imports in Python