- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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[1]/div/div[1]/a. It can be verified with the expression $x("/html/body/div[1]/div/div[1]/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.
- Related Articles
- What is the primary difference between the XPath and CSS selector in Selenium?
- Difference between an Absolute URL and a Relative URL
- What is xpath in Selenium?
- 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 and automation?
- What is the difference between mocha and Selenium?
- What is difference between selenium 1 and Selenium 2
- What is difference between selenium 2 and selenium 3?
- Absolute and Relative Imports in Python
- Absolute and Relative frequency in Pandas
- CSS Absolute and Relative Units
- What is the difference between getWindowHandle() and getWindowHandles() in Selenium?
- What is the difference between Selenium RC and Webdriver?
- What is the difference between selenium WebDriver and TestNG?
