Xpath is one the locators used in Selenium to identify elements uniquely on a web page. It traverses the DOM to reach the desired element having a particular attribute with/without tagname.
The xpath can represented by the ways listed below −
The xpath can be created by the following methods −
OR & AND expression.
following- sibling expression.
There are two types of xpath – absolute and relative.
Relative xpath – This path begins from any part of the DOM html. It is represented by double slash // and helps to identify elements from any part of the web page and the xpath expression is not a lengthy one.
It is always better to use relative xpath since we need not start from the root to locate our element. So in case any attributes or their values are getting modified, the probability of getting affected to an incorrect xpath is less.
Absolute xpath – This path starts from the root html to the desired element. It is represented by single slash /. It is lengthier compared to relative xpath and if any of the attributes or their values starting from the root element get changed, then we end up not being able to uniquely identify our desired object on the page.
Xpath can perform bidirectional flow which means the traversal can be both ways from parent to child and vice- versa.
In terms of speed, xpath is slower compared to css.