Introduction to XPath

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.

Example:

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

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

XML Code:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

/bookstore/book[@category='Chemistry']/author

Syntax of XPath:

//tagname[@attribute = ‘value’]

XPath Expressions:

Symbol Description
// 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:
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.

  • Example:
    /html[1]/body[1]/div[6]/div[1]/div[3]/div[1]/div[1]/div[1]/div[3]/ul[1]/li[2]/a[1]

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.

  • Example:
    //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()
  • Start-With()
  • Text()

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

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

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.
Example:

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

text(): This function is used to find the node having the exact match with the specified string value in the function.
Example:

//div

Uses of AND and OR in XPath
AND and OR are used to combine two or more conditions to find the node.
Example:

//input[@value = 'Log In' or @type = 'submit']


Similarlly, We can apply AND operator in XPath.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.