Updated April 8, 2023
What is XPath in Selenium?
Xpath or XML Path is an XML expression tag used for finding the elements in a web page while programming test scripts in Selenium Automation Tool. It can be used to find the position element’s tags like element’s name, element’s ID, link text, a dynamic element using Xpath, CSS path, element’s class name, etc. On the web page of the application being tested using selenium automation. There are two types of Xpath used, namely Absolute Xpath and Realtive Xpath.
Syntax of XPath in Selenium
Below given is the syntax of XPath in selenium:
Xpath = //Tagname[@Atrribute=value]
- //: Used to select the current node.
- Tagname: Specifies the tag name of a particular node like img, div, input, etc.
- @: Selects the attribute.
- Attribute: Specifies the attribute name of the node.
- Value: Specifies the values of attributes of the node.
Various Locators are used in Xpath to locate the web elements:
- Id: Finds the web element by the id of that element. (Id is unique for every web element).
- Name: Finds the web element by the name of that element.
- Class Name: Finds the web element by the class name of that element.
- Link Text: Finds the web element by the text written on the link of that element.
- CSS path: Elements with no name, id, and class name are found through the CSS path created by testers/ developers.
Types of XPath in Selenium
There are basically two types of XPath in Selenium which are described below:
1. Absolute XPath
It is one of the easiest and direct ways to locate a web element in a document. It selects an element path from the root node and takes the path until the element covering all the sections in a document, including div, section, li, etc. It starts with a single forward slash ‘/’ specifying the root level. The only disadvantage of using the Absolute Path in Selenium is that if any changes are made in the website or a document, then the whole XPath of that element changes, and hence the previous path will not work, and the whole program will get failed.
If any changes made in the HTML document and div elements’ position have been changed, then the previous XPath will get failed.
2. Relative XPath
One of the disadvantages of Absolute XPath, apart from modification, is too lengthy paths; relative paths are a savior in such situations. Relative Path is used to define an element’s path from the middle of the HTML DOM structure. Unlike Absolute XPaths, it is not necessary to define the path from the root level, which in turn reduces the overall length of XPath. It starts with double forward slash ‘//’ ,i.e. Selecting the web element from anywhere in the document.
//input[@id = 'email']/div/li/a
XPath Functions in Selenium
Below given are some of the functions of XPath used in Selenium:
1. Contains(): It is a method that is used in XPath expression and is used to locate the element with the partial text in the case where the text to be searched has too lengthy, and the value of attribute changes dynamically on reload.
Xpath = //*[contains(@name='inputButton')]
2. Following: This method selects all the current node elements in the HTML DOM structure.
3. Ancestor: This method is used to select all the ancestor elements from the current node. The ancestor can be grandparents, parents of the current node in the HTML document.
It will find all the ancestors of the current web element match the text criteria ()= ‘Introduction’ having the div tag.
4. Child: This method is used in the scenarios in which we want to select the current node’s child elements in the HTML document.
5. Proceeding: This method is used to select all the nodes proceeding to the current node or the ones that come before the current node.
6. Following-sibling: This method is used to locate the siblings who are at the same level as the current node.
7. Parent: This method is used to locate the current node’s parent in the HTML document.
8. Self: This method is used to locate itself in the HTML document. Self signifies the current node only.
9. Descendant: This method is used to find the current node’s descendants in the HTML document.
10. Start-with: This method is used in the scenarios when we want to match the starting text of the attributes and when we need to locate the web element when the attribute changes dynamically on refreshing and reload a web page.
The above mentioned are some of the XPath types and the various functions used in Selenium for XPath. These functions make it easy to work with XPaths as the foremost step for automating locating the web element through XPath. Various locators such as id, name, CSS path, link text are used in the XPath to locate web elements in the web page. Knowledge and different ways of finding XPath are very important when working with automation testing using either Selenium or any other tool.
We hope that this EDUCBA information on “what is xpath in selenium?” was beneficial to you. You can view EDUCBA’s recommended articles for more information.