You must be thinking now that if relative xpath is so much better then why there is even need of an absolute xpath? Because in real world nothing is perfect. It is much more shorter than absolute xpath. Now below image is of the relative xpath. We can clearly see how lengthy it is and if any of those nested relationships is changed then the locator will not function The below image is the absolute xpath of search bar. Let’s write both absolute and relative xpath for a google search bar. It is always advisable to use relative xpath as it is shorter and if any element added/removed from DOM, then absolute xpath will not work but relative xpath will continue to point to that particular element. It means, we can start from any node we want to. Relative xpath: It uses a reference of nodes to traverse through DOM. It uses single slash(/) only and establish an absolute relationship between nodes, i.e. There are 2 types of xpaths: Absolute and Relative.Ībsolute xpath: It is a complete path from root element to desired element. In the above example, // is the starting point, input is the Tag name, name is the attribute and its value is ‘s’. Value is the actual value of that particular attribute. ,, is used to select the particular attribute of node.Īttribute is the attribute name of the node. It is the starting point from where navigation will happen in HTML page to find the element. The basic syntax of xpath is:ĭouble slash(//) is used to select the current node. Xpath uses Path like string expression to traverse through XML document and identify elements. In companies, most of the engineers prefer xpath over any other method because they often have to deal with such elements which can not be located via any other way.įirst of all, what is Xpath? Xpath stands for XML Path Language. Today we will go in depth to cover as much as possible about writing an effective xpath as this is one of the most crucial skill for a selenium tester to be able to write a good xpath. In the last post, we learned about the methods to find web elements on a web page and got a glimpse of xpath.