Welcome to Selenium tutorial, in this post, we will see how to find xpath in Chrome browser for Selenium script development.
Till now we have seen that we have so many plugins for xpath in Firefox Browser like Firebug and Firepath but for Chrome, IE and another browser we do not have a plugin to identify xpath.
In the last post,we have seen Awesome plugin in Firefox for xpath and if you still have not used then please give a try. 🙂
Today I was googling something and found one good post for Xpath in chrome browser and I installed the same on my machine and it worked easily so thought of sharing with all.
This plugin will not generate xpath like firebug and other plugin do but yes, you can verify your xpath at least.
Find xpath in Chrome browser
Install Xpath Helper plugin for Chrome.
1- Open below URL and Click on Add to Chrome button.
Xpath Helper- https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en
Step 2- Once plugin added to Chrome. Open your application and press SHIFT+CTRL+X. Black window will appear on top then you can play with xpath. Refer below screenshot for more details.
On the top left page, you can write xpath and the right-hand side you can check the result. If single result found then it will show result 1 and if it matches with multiple items then it will show you count as well.
If xpath is invalid- then you will get xpath error and if no matches found then it will display NULL.
Note- To check element attribute still you can use F12 that is nothing but default inspect element for all browser then check attribute and create your own xpath and try.
Xpath Syntax
// tagname[@attribute-name=’value1′] and if you are not sure about tag name then no worry you can try with * also
// *[@attribute-name=’value1′]
Thanks for visiting my blog. If you find this post informative then please share with your friends.
For more updates like our Facebook Page.
For any Selenium related query Join Facebook Group.
lakshmi says
Hi mukesh, can u pls tell me how to write xpathusing href
Mukesh Otwani says
Hi Lakshmi,
Same way you do for any other attributes like //a[@herf=’value_of_href_available_in_DOM’]
Harshitha says
If I take gmail in that new user registration form and login form will be der. So can I write those two in one script?
If it s pls let me know the code
Mukesh Otwani says
Hi Harshitha,
It depends on how you much granularity you require for functionality test and what is your business requirement. If you are using testng then either you can define both of these functionality under different @Test annotations in same script or you can make different scripts for same. Another thing, how you will execute these scripts, I mean using some application like Jenkins or Bamboo or so on.
Mahul says
Thanks Mukesh Sir,
There is one xpath helper for chrome browser i.e. Relative Xpath Helper.
This will be added as a extension in chrome.
We are using this extension to find the xpath on chrome browser and its very helpful.
Mukesh Otwani says
Hi Mahul,
I am happy to see my blog post has helped you.
Nikhil says
Mukesh even I tried tool, but it took time to adjust, but compared to firepath its difficult to find Xpath on chrome, any idea.
Mukesh Otwani says
No updates on this.
Ranjith samalla says
Hi Mukesh,
Good Morning, Today i tried xpath for chrome but when I’m clicking on ctl+shift+x or i’m selecting through icon but how can we generet xpath still i’m gettiing confuse can u xpln clrly or els can u make video 4r this,
Mukesh Otwani says
Seems this plugin is outdated.
Phani says
Ranjith, Here you can check if your xpath is correct or not in this tool. You can even see the if multiple items matches. I would say this extension is great checking tool rather generating tool. Some one please correct me if I’m wrong.
Thanks.
Gaurav Khurana says
Yes i agree. this is a nice tool for testing xpath.
Xpath can be generated by pressing the shift button and selecting the item. But the xpath generated is the absolute and is not very friendly.
Thanks Mukesh..
i have found one of the best way on stackoverflow
up vote
5
down vote
Let tell you a simple formula to find xpath of any element:
1- Open site in browser
2- Select element and right click on it
3- Click inspect element option
4- Right click on selected html
5- choose option to copy xpath Use it where ever you need it
you can include this … its simple and awesome
Mukesh Otwani says
Thanks Gaurav
Komal says
Thanks Gaurav for such useful information.
Really helpful.:)
Mukesh Otwani says
Thanks Komal 🙂 Keep visiting.