In Selenium automation, Locators are used to locating the elements on webpages but very often it has been observed that either multiple matching elements are located or not even any element is located. So, in order to accurately locate the elements on the webpage, the XPath locator is used.
There are different types of locator in selenium which we can use and find the web elements.
- Class Name
- Tag Name
- Link text and Partial Link Text
- CSS Selector
XPath is defined as an XML path. It helps in locating any element on the webpage using an XML path expression. The fundamental syntax of XPath which is explained below with screenshot.
Types of X-path
1) Absolute XPath
2) Relative XPath
- Absolute XPath begins with a single forward slash (/), which means you can select the element from the root node.
- It uses a complete path from the Root Element to the desired element.
- The advantage of using absolute is, the identification of the element becomes very fast.
The disadvantage is, if anything goes wrong like some other tag added or removed in between, the path will no longer work.
- Relative XPath starts from the node of your choice. It is not necessary to start from the root node.
- It starts with the double forward slash (//) which indicates it can search the element anywhere on the webpage.
- If the same path consists of multiple elements, it will select the first element that is identified.
- The advantage is, no need to write long XPath.
Handling dynamic elements in Selenium Using XPath
1. Basic XPath:
- XPath expression select nodes based on attributes like ID, Name, Classname, etc.
2. Contains ():
- With the dynamically changing values, contains () is used prominently.
- The contain feature is used to locate the elements with partial text.
- In OR expression, from two conditions, any one condition should be true to find the element.
- In AND expression, from two conditions, both conditions should be true to find the element.
- This is used when the initial partial attribute value of initial text associated with the web element is fixed. Which consists of both static (initial) and dynamic (trailing) values.
- The elements with the exact text match can be found as mentioned below. The Text () is a case sensitive. In our example, we find the element with the text “Sign in”.
- It is used to locate the element node at the specified position of the specified node type.
- //center/input[position( )>1] :
2nd no. (I’m Feeling Lucky) input button highlight and 1 matching node.
- //center/input[position( )>0] :
1st no. (Google search) and 2nd no. (I’m Feeling Lucky) input button highlight and 2 matching nodes.
- //center/input[position()>0 and position()<3] :
2 matching node and both input button highlighted.
- In order to exclude HTML Tag, then you need to use ‘*’
- In order to include HTML Tag, then do not need to use ‘*’
Below both images verify matching node for same path and text.
- In 1st Example, when we use HTML tag(p) then first check HTML tag(p) after going into HTML tag’s attribute (we use text () method) and match with attribute’s value (Sign in) and select “Sign in” text.
- In 2nd Example, we use * sign and then check in “Sign in” text in whole HTML Document.
Firebug and fire path plugin are not supported in the later versions of Firefox 56. So you can find XPath in Google Chrome.
- Right-click on any element and click on “Inspect element”.
- Press “Ctrl+F” key and type relative XPath and find the element.