XPath(XML Path) is an expression which is used to find the element or say node in the XML document. In Selenium it is commonly used to find the web elements.
//input[@id = 'fakebox-input']
In this example, We are locating the ‘input’ element whose ‘id’ is equal to ‘fakebox-input’
The XML Code is a tree like structure as we can see in the above XML, the code starts with the bookstore node that has a child node book and it is followed by an attribute category whose value is ‘Math’. The book node has 2 child node i.e. title and author.
To select the author element of the chemistry book, the following XPath will be used:
Syntax of XPath:
//tagname[@attribute = ‘value’]
|//||Selects nodes in the document from the current node that match the selection no matter where they are|
|/||Selects the root node|
|tagname||Tag name of the current node|
|@||Select the attribute|
|attribute||Attribute name of the node|
|value||Value of the attribute|
Types of XPath:
- Absolute XPath
- Relative Xpath
Absolute XPath uses the root element of the HTML/XML code and followed by all the elements which are necessary to reach the desired element. It starts with the forward slash ‘/’ . Generally, Absolute XPath is not recommended because in future any of the web element when added or removed then Absolute XPath changes.
In this, XPath begins with the double forward slash ‘//’ which means it can search the element anywhere in the Webpage. Generally Relative Xpath is preferred as they are not complete path from Root node.
//input[@id = 'fakebox-input']
If you want to learn how to make XPath identify webelements, Then open the webpage in chrome browser and inspect the element by right click on the webpage and after that press ‘ctrl+f’ to find the webelements using XPath. You can also use the chrome extension like ‘chropath’ to find the xpath for a webelement.
Commonly Used XPath Functions:
contains(): This Function is used to select the node whose specified attribute value contains the specified string provided in the function argument.
starts-with(): This function is used to select the node whose specified attribute value starts with the specified string value provided in the function arguments.
text(): This function is used to find the node having the exact match with the specified string value in the function.
Uses of AND and OR in XPath
AND and OR are used to combine two or more conditions to find the node.
//input[@value = 'Log In' or @type = 'submit']
Similarlly, We can apply AND operator in XPath.
- PHP | SimpleXMLElement XPath() Function
- p5.js | Introduction
- CSS Introduction
- Introduction to ES6
- Introduction to KnockoutJS
- Ajax | Introduction
- Bootstrap 4 | Introduction
- PHP | Spreadsheet_Excel_Writer | Introduction
- Introduction to Express
- HTML5 | Introduction
- Introduction to AngularJS
- Introduction and Installation of Git
- XHTML | Introduction
- REST API (Introduction)
- Introduction To AWS Lambda
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.