The XPath is a navigation tool for XML documents that is used to traverse and locate elements in an XML file. However, XPath is not an exclusive tool for XML files/documents only, it can be used to travel through any hierarchical markup file/document (XML, HTML, etc).
Types of XPath
There are multiple ways to express an XPath. The major types of XPath are separate according to the way they refer to the elements (nodes) in the document. The two major types of XPath are:
Absolute XPath — The path starts from the root node of the document (the body element by default in a typical HTML file).
Relative XPath — The path starts from any specified node of the user (the root node is defined by the user, primarily done by specifying the element type after the // symbol).
Basic structure of XPath There are seven types of nodes used in XPath. They are as follows: element, attribute, text, namespace, processing-instruction, comment and document nodes. XML documents are known as trees of nodes. The topmost element of the tree is called the root element.
// refers to the current element (for absolute XPath, it’s normally the default root node of the document, and any recognizable element for relative XPath)
tagName is the name of the specified root node. This can be user-specified (relative XPath) or it can be either body or head (for HTML and XML docs — Absolute XPath).
 is used to move down hierarchically from the root into the document (if possible).
@ specifies that an attribute will be used to identify the desired element.
attribute refers to a known and accessible attribute of the element in question.
Value is a string literal that is used as a validation string so that the XPath selects the correct element.
XPath in Selenium The XPath system in Selenium is used to get the location of nodes pointing to the elements such that the located elements can be used for means of automation. In essence, one can use XPath to access the element in a specific webpage and then use Selenium’s tools to manipulate those elements.
Customized XPath in Selenium Not all XPaths created will or should match every specific use-case for it. So, in many cases, there are paths which are arguably better and serve the requirement better than the auto generated or the complete one.
Example of a customized XPathThere are a myriad of customization options that XPath offers to aid in automation. Some of the more useful and effective ones are as follows:
xpath = "//li[@id=’ca-view’]" xpath = "//a[text()='Feature']" xpath = "//label[@class=’selected mw-list-item collapsible’]" xpath= "//ul[contains(text(), 'Sign in']" xpath= "//input[@name='site-header']" xpath= "//a[input(text(),'Login')]" xpath= "//button[@type='button' and @class='btn']" xpath= "//a[@title='Get-in-touch' or @id='menu-item-95']" xpath= "//div[@class='dropdown']//button[@type='btn' and @class='button btn-secondary dropdown-toggle' and @id=dropdownMenuButton']"
Summary XPath is a very powerful and vast topic. I tried to condense the major and important parts into a more readable format through this blog post. While very powerful, the XPath is not a monolithic concept. There exists multiple options for customization and configuration ranging from locating the element or node by utilizing the ID, Name or CSS locator attributes. They are said to be the most popular and fast locators. However, it is not possible to locate the element by id, name or css selectors all the time. In such cases XPath in selenium plays a vital role to locate the actual and dynamic element.
Happy Testing !!