Open In App
Related Articles

Introduction to XPath

Like Article
Save Article
Report issue

XPath(XML Path) is an expression that is used to find the element or say node in the XML document. In Selenium it is commonly used to find the web elements.

What is XPath?

XPath stands for XML Path Language. It’s an expression language that is used to query or transform. We use it to traverse among elements and attributes in an XML document. The World Wide Web Consortium (W3C) defined XPath in 1999.

XPath is used to:

  • Query or transform XML documents
  • Traverse elements, attributes, and text through an XML document
  • Find particular elements or attributes with matching patterns
  • Uniquely identify or address parts of an XML document
  • Extract information from any part of an XML document
  • Test addressed nodes within a document to determine whether they match a pattern


//tagname[@attribute = ‘value’]

XPath Expressions:

//Selects nodes in the document from the current node that match the selection no matter where they are
/Selects the root node
tagnameTag name of the current node
@Select the attribute
attributeAttribute name of the node
valueValue of the attribute

Example: In this example, We are locating the ‘input‘ element whose ‘id‘ is equal to ‘fakebox-input

//input[@id = 'fakebox-input']

XML Code: 


<?xml version="1.0" encoding="UTF-8"?>
<book category = "Math">
    <title lang="en">IIT Mathematics</title>
    <author>A Das Gupta</author>
<book category = "Chemistry">
    <title lang="en"> Inorganic chemistry for JEE</title>
    <author>V K Jaiswal</author>


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:


Types of XPath:

  1. Absolute XPath
  2. Relative Xpath

1. Absolute 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.



2. Relative XPath; 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:

  1. contains()
  2. Start-With()
  3. Text()

1. contains(): This Function is used to select the node whose specified attribute value contains the specified string provided in the function argument.


//input[contains(@id, 'fakebox')]

2. 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.


//input[starts-with(@id, 'fakebox')]

3. 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']

Similarly, We can apply AND operator in XPath.

Last Updated : 27 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads