What is XPath in Selenium?
As we all know that now with the change in the trend in the Software Industry, scope of manual testing has been minimized and more of the test cases are automated as it not only increases efficiency and accuracy but also reduces time. Many software tools are available in the market for the development and execution of automated test scripts. Selenium is one of them. Selenium is open-source, and perhaps the most widely used automation testing suite used for automation testing. It supports a variety of languages like Php, Java, Perl, Python, etc. by installing the drivers of particular languages. It also supports multiple browsers like Chrome, Mozilla Firefox, IE1, Edge, Opera browser, etc.
XPath stands for XML Path Language. In Selenium, XPath is nothing but a normal XML path that is used to locate any element in the web page using XML path expression. For performing any operation like clicking on the button, writing in a textbox, selecting any field, etc the first thing is to find the location of that web element in the document and in Selenium, Xpath helps to locate that path. XPath uses the HTML DOM structure while gathering the path of any element in a webpage. It has path expression written along with some conditions. XPath is a W3C recommendation,
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 having no name, id, class name are find out 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 till 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 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 the position of div elements has been changed then the previous XPath will get failed.
4.5 (836 ratings)
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 the path of an element 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 which 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 elements of the current node 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 matches the criteria of text()= ‘Introduction’ having the div tag.
4. Child: This method is used in the scenarios in which we want to select the child elements of the current node 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 which are at the same level as the current node.
7. Parent: This method is used to locate the parent of the current node 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 descendants of the current node 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.
Above mentioned are some of the XPath types and the various functions used in Selenium for XPath. These functions make 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.
This is a guide to What is XPath in Selenium?. Here we discuss the basic concept, syntax, types, and Functions of XPath in Selenium with its examples. You may also look at the following article to learn more –