What is XPath

Xpath is nothing but XML path, develeopper used xpath to validate XML files. HTML also follows same structures as XML, so we can apply xpath to HTML pages as well.

Hope reader is familiar with TryXpath and develpper tools of browser which we use for inspecting elements and verify out xpaths

Xpath is nothing but sting expression which used to find the element(s) along with automation tools

We should give last priority to XPath among locators because Xpath is little slow compared with other locators, if we are not able to find the element with id, name, linktext, css then only we should go for xpath

Selenium support xpath 1.0 and xpath 2.0 is not compatible with selenium

In this tutorial we are going to learn how to build xpaths and verify xpaths

Syntax of XPath

Xpath follows very simple syntax, please find below image for the xpath syntax


Xpath Syntax
xpath-selenium-selenide
HTML code Syntax
html-code-xpath-selenium

HTML code can have n-number of attribute, Text and closing tag is not mandatory for few elements


There are two kind of xpaths
  1. Absolute xpath
  2. Relative xpath

Absolute Xpath
absolute-xpath-selenium-selenide / - point to first node on the html document, it is html tag
Note: we are not going to focus on absolute xpath.

Relative Xpath
relative-xpath-selenium-selenide // - points to the any node in the webpage
tagName - tag name is nothing but the name which is present after the < (angular bracket)
attribute - whatever is present inside < and > baracket except tagname is attribute, any number of attributes can present in html code
attribute's value - it is corresponding value to the attribute, sometime for boolean attribute developers may not specify any value, in those cases html takes 'true' as defaut value.
Text - text is the value present inside > and <

Now let form the xpath for the above html code.
//a[@class='idle']

Xpath with Tagname

We can write Xpath based on the Tagname, which is very simple.


							Syntax for Xpath with Tagname : //tagName
							

							<html>
								<body>
									<div id="pancakes">
										<button type="button">Blueberry</button><br><br>
									</div>
								</body>
							</html>
							
In above code there is button present under div. we can write the xpath with tagname : //button

Xpath with Index

We may not see unique elements in the webpage, other than on login page. Please save the below html file as composite-xpath.html on you local machine


							<html>
								<body>
									<div id="pancakes">
										<button type="button">Blueberry</button><br><br>
										<button type="button">Banana</button><br><br>
										<button type="button">Strawberry</button><br><br>
									</div>
								</body>
							</html>
							

Open above html file in chrome, and press F12 or right click on element and choose Inspect Element or Press Ctrl+Shift+I

It may look like below image once you open the chrome developer tool

composite-xpath-selenium

Press Ctrl+F to verify Xpath, and write the xpath based on the xpath syntax.

Xpath based on the Tagname : //button 3-matches-composite-xpath

When you try the xpath with tagname it shows three matches, so we cannot proceed as we want to find only one match. We must write a xpath expression which should hav only one match. When we have matching element only under one parent(this case), we should add index to the xpath


							Syntax for Xpath with Index : //tagName[index]
							


index must be covered with square('[',']') brackets. Index starts from 1 in xpath index-xpath-selenium Xpath for the elements :
  Bluberry button- //button[1]
  Banana button - //button[2]
  Strawberry button -//button[3]

Xpath with Attribute

we can use index type xpath when we have more matches under one parent, index might not work if there are more parent

Store below html in local system and open it with chrome


							<html>
								<body>
									<div id="pancakes">
										<button type="button">Blueberry</button><br><br>
										<button type="button" name='banana' >Banana</button><br><br>
										<button type="button">Strawberry</button><br><br>
									</div>
									<div id="pancakes">
										<button type="button">Apple</button><br><br>
										<button type="button">Orange</button><br><br>
										<button type="button">Grape</button><br><br>
									</div>
								</body>
							</html>
							

Let's try to write xpath for Banana button, Xpath based on index is //button[2] but it has two matches 1. Banana, 2.Orange.

With index we may not be able to solve this issue.

Let's consider other properties of the html element, banana has attribute name, Now we have to form the xpath based on the attribute.


							Xpath with Attribute ://tagName[@attribute='attribute value']
							

Xpath based on the Attribute is : //button[@name='banana'] , this xpath shows only one match which is Banana button

You can add n number attributes in one xpath itself

							Xpath with multiple Attributes://tagName[@attrib='attrib value'][@attrib2='attrib2 value']...
							

Can I use index along with attribute: yes , you can use, but index will be usefull only when matches are under single parent


							Xpath with Attribute and Index://tagName[@attribute='attribute value'][index]
							

Xpath With Parent Reference

We cannot expect a HTML element to have different or uniques properties all the time, sometime there is a chance that every element may have same kind of attributes, In those cases we cannot use Xpath with Attribute

To handle such kind of cases we may need to take help of the parent element to find our actual element

Store the below code in html file and open it in chrome


								<html>
									<body>
										<div id="berry">
											<button type="button">Blueberry</button><br><br>
											<button type="button">Banana</button><br><br>
											<button type="button">Strawberry</button><br><br>
										</div>
										<div id="fruit">
											<button type="button">Apple</button><br><br>
											<button type="button">Orange</button><br><br>
											<button type="button">Grape</button><br><br>
										</div>
									</body>
								</html>
								

Let's write xpath for Orange, using parent and child concept


Syntax for Xpath with parent and child
parent-child-xpath

For Orange element we have to refet]r it parent div which has id attribute as fruit Xpath for the Orange: //div[@id='fruit']/button[2] parent-child-xpath-selenium We have only one match for the xpath we have written.

Explanation for Xpath : //div[@id='fruit']/button[2]
// - look for any node which has 'div' as tagname and id as fruit, look for immediate child(/) node which has tagname as button and at the index of 2.

Xpath with Group Index

Sometimes we may have to handle th elements with xpath index but index may give more than one match, which are under different parents, in these situations index might not help you. We may have to use Group index in these kind of scenarios

Group index puts all matches into a list and gives indexes them. So here we will not have any duplicates matches


							Syntax : (//tagName)[index]
							

We have to use paranthesis to make a xpath into group xpath after it index the xpath

Store below HTML code in to html file :

							<html>
							<body>
								<div id="fruit"><br><br><br>
									<button type="button">Blueberry</button><br><br>
									<button type="button"  >Banana</button><br><br>
									<button type="button">Strawberry</button><br><br>
								</div>
								<div id="fruit">
									<button type="button">Apple</button><br><br>
									<button type="button" >Orange</button><br><br>
									<button type="button">Grape</button><br><br>
								</div>
							</body>
						</html>
							

Let's write xpath for Orange : (//button)[5]

group-index-xpath-selenium-webdriver

text() function in Xpath

There will e situation where you may not able to use any html property ther than text present in the element

text() function helps us to find the element based on the text present in the element, text() funnction is case sensitive


								<button type="button">Blueberry</button><br><br>
								
In above code the text is Blueberry, and we can write xpath using text() like below

								xpath with text : //button[text()='Bluberry']
								

Note: we use @ sign for attributes, functions donot need @ sign

Dependent and Independent Xpath

We may face scenarios where the give element may change it's position everytime, so handle such kind of scenarios we have to go for dependent and independet xpaths

For example : Take any ecommerce website, search for a specific product and write xpath for that particular product, take rest for few days and then go and search for the same product, there could be change in position of the product, to handle this we should use dependent and independent concept

Scenario : Select the check box which is present in the same row as Protractor

Select Tool Language
Selenium Java
Protractor Typescript
Selenium Bindings Python
QTP VB

Steps to solve the scenario:
1. Donot write the xpath for the checkbox, beacuse checkboxes might change its position.

2. Based on the text present in the Protarctor field we have to write the xpath

2.1 we have to find the common parent for Protractor and Checkbox

3. Xpath to find the protractor : //td[text()='Protractor']

4. Now we should find the parent of Protractor element

5. We can find the parent of an elemen using /.. like in unix

6. Xpath for parent of Protractor : //td[text()='Protractor']/..

7. Check Protractor's parent is common parent for Protractor and checkbox.

8. Yes, Protractor parent is common parent for Protractor and checkbox

9. Now try to navigate to checkbox using checkbox properties

10. Checkbox has tagname as inpu : //td[text()='Protractor']/..//input

11. try the above xpath it will highlight the checkbox related to Protractor field

Here Protractor is independent and checkbox is dependent
Independent: It doesnot depend on any other element
Depenedent : We have to find this based on the other element(Independent)

contains() function in Xpath

contains() function helps user to fins the element with partial values, or dynamically chaning values, contains verifies matches with portion of the value


							contains function ://xpath[contains(@attribute, 'attribute value')]
													//xpath[contains(@text(), 'attribute value')]
							
Example of below html:

							<html>
							<body>
								<div id="fruit"><br><br><br><br><br><br><br><br><br><br><br>
									<button type="button">Blue berry1234</button><br><br>
									<button type="button" >Banana</button><br><br>
									<button type="button">Straw</button><br><br>
									<button type="button">berry</button><br><br>
									<button type="button">Straw berry</button><br><br>
									</div>
								</body>
							</html>
							


Xpath for the Blueberry : //button[contains(text(),'Blue')]
Xpath for the Banana : //button[contains(text(),'Ban')]

More Complex items:
In same way if you try to find xpath for Straw berry with //button[contains(text(),'Straw')] it find the element with text Straw as well.

If you try with berry you may get 'berry' element. So how to find teh Straw berry button.

We can combne more than one contains functions like : //xpath[contains(text(), 'text1')][contains(text(), 'text2')]
Xpath for Strawberry is : //button[contains(text(),'Straw')][contains(text(), 'berry')]

Not only for text you can apply contains function for other properties as well Eg : //button[contains(@type,'but')]

Normalize Space in Xpath

Normalize space matches the element ignoring starting and ending spaces


							syntax ://xpath[normalize-space(property)='value']
							

							<button type="button">   Strawberry   </button><br><br>
							

Xpath for the Strawberry element : //button[normalize-space(text())= 'Strawberry']

starts-with func xpath

Starts-with function matches the elements which property starting value


							syntax ://xpath[starts-with(@attribute,'starting value')]
							

							<button type="button">Straw berry</button><br><br>
							
Xpath for the Strawberry element : //button[starts-with(text(), 'Straw')]

ends-with func xpath

Ends-with function matches the elements properties ending value


							syntax ://xpath[ends-with(@attribute,'ending value')]
							

							<button type="button">Straw berry</button><br><br>
							

Xpath for the Strawberry element : //button[ends-with(text(), 'berry')]

Last() function in Xpath

By default automation tools takes first instance of the match, also if we want to achieve the first element we can use index [1].But in some pages we may not be able to see how may matches are present when page is loading or on dynamic page.

last() function in Xpath helps user to find the last match of the element.


							last function : //xpath[last()]
							
take example of below html code

							<html>
							<body>
								<div id="fruit"><br><br><br>
									<button type="button">Blueberry</button><br><br>
									<button type="button"  >Banana</button><br><br>
									<button type="button">Strawberry</button><br><br>
								</div>
							</body>
						</html>
							

In above if we want to write xpath for last element it is easy we can say use index [3] , but if application is very large or dynamic, we cannot say how may elements are gonna present.

So let's use last funtion in xpath : //button[last()] - points to the Strawberry button.

last-xpath-selenium

Position function in Xpath

Position function help the user to get the match at particular index, using position we can get element which are less than the postion or greater than the position as well.


								position function ://xpath[position()=2]
														://xpath[position()<2]
														 ://xpath[position()>2]
														 ://xpath[position()=<2]]  ...
								
								
Example : //button[position()=2] position-xpath-selenium

You can also share knowledge by Adding a topic here



Comment / Suggestion Section

Point our Mistakes and Post Your Suggestions