XPath with selenium python

Xpath is nothing but XML path, the developer used XPath to validate XML files. HTML also follows same structures as XML, so we can apply XPath to HTML pages as well along with selenium webdriver.

Hope reader is familiar with TryXpath and developer 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 Selenium Webdriver, and other 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 supports XPath 1.0 and XPath 2.0, 3.0 are 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 attributes, Text and closing tag is not mandatory for a few elements


There are two kinds of xpaths
  1. Absolute XPath
  2. Relative XPath

Absolute Xpath

absolute-xpath-selenium-selenide

/ - point to the 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 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 > bracket except tagname is an attribute, any number of attributes can present in HTML code

attribute's value - it is corresponding value to the attribute, sometimes for boolean attribute developers may not specify any value, in those cases, HTML takes 'true' as default 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 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 the 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 your local machine

<html>
	<body>
		<div id="pancakes">
			<button type="button">Blueberry</button><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 the element and choose Inspect Elementor Press Ctrl+Shift+I

It may look like the 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



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 an XPath expression which should have only one match. When we have a matching element only under one parent(this case), we should add an 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 with webdriver when we have more matches under one parent, the index might not work if there are more parent

Store below HTML in the local system and open it with chrome

​
We can use index type xpath with webdriver 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]

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

With an 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 the index will be useful only when matches are under a single parent

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

Xpath With Parent Reference

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

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


The 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 the elements with XPath index but the 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 this 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 parenthesis to make an xpath into group XPath after it indexes the XPath

Store below HTML code into 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 be situations, where you may not able to use any HTML property other than text present in the element

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

<button type="button">Blueberry</button><br><br>​

In the 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 do not need @ sign

We can also match element(s) which have text in them with below XPath

xpath with text ://button[text()]
                 ://button/text()

Wild card Character with Xpath in Selenium python

* -is the one of most used wild card character with xpath in selenium webdriver, we can use it instead of tag name and attribute

//* - matches all the elements present in the html (including html)

//div/* - matches all the immediate element(s) inside the div tag

//input[@*] - matches all the element(s) with input tag and have atleast one attribute, attribute value may or maynot present

//*[@*] - matches all the element(s) which have atleast one attribute.

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')]

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions