Monday, December 15, 2008

Way to get XPATH in Internet Explorer

Are you stuck with an application that only works in IE?
Do you want the XPATH of an element that can be seen only IE? Unable to replicate the scenario in firefox. There are many firefox addons like xpather, xpath-checker and firebug that will give you the xpath of an element in a second. But sadly there is no addon or tool avaialable that will do this for IE. For most cases you can get the xpath of the elements that fall in your script using the above tools in firefox and tweak them a little (if required) to make them work in IE. But if you are testing an application that will work only in IE or the specific scenario or page that has this element will open-up/play-out only in IE then you cannot use any of the above mentione tools to find the XPATH.
Well the only thing that works in this case is the Bookmarklets that were coded just for this purpose. Bookmarklets are JavaScript code that you will add in IE as bookmarks and later use to get the XPATH of the element you desire. Using these you can get the XPATH as easily as you get using xpather or any other firefox addon.

STEPS TO INSTAL BOOKMARKLETS
1)Open IE
2)Type about:blank in the address bar and hit enter
3)From Favorites main menu select--->Add favorites
4) In the Add a favorite popup window enter name GetXPATH1.
5)Click add button in the add a favorite popup window.
6)Open the Favorites menu and right click the newly added favorite and select properties option.
7)GetXPATH1 Properties will open up. Select the web Document Tab.
8)Enter the following in the URL field.

javascript:function getNode(node){var nodeExpr=node.tagName;if(!nodeExpr)return null;if(node.id!=''){nodeExpr+="[@id='"+node.id+"']";return "/"+nodeExpr;}var rank=1;var ps=node.previousSibling;while(ps){if(ps.tagName==node.tagName){rank++;}ps=ps.previousSibling;}if(rank>1){nodeExpr+='['+rank+']';}else{var ns=node.nextSibling;while(ns){if(ns.tagName==node.tagName){nodeExpr+='[1]';break;}ns=ns.nextSibling;}}return nodeExpr;}

9)Click Ok. Click YES on the popup alert.
10)Add another favorite by following steps 3 to 5, Name this favorite GetXPATH2 (step4)
11)Repeat steps 6 and 7 for GetXPATH2 that you just created.
12)Enter the following in the URL field for GetXPATH2

javascript:function o__o(){var currentNode=document.selection.createRange().parentElement();var path=[];while(currentNode){var pe=getNode(currentNode);if(pe){path.push(pe);if(pe.indexOf('@id')!=-1)break;}currentNode=currentNode.parentNode;}var xpath="/"+path.reverse().join('/');clipboardData.setData("Text", xpath);}o__o();

13)Repeat Step 9.

You are all done!!

Now to get the XPATH of elements just select the element with your mouse. This would involve clicking the left mouse button just before the element (link, button, image, checkbox, text etc) begins and dragging it till the element ends. Once you do this first select the favorite GetXPATH1 from the favorites menu and then select the second favorite GetXPATH2. At his point you will get a confirmation, hit allow access button. Now open up a notepad file, right click and select paste option. This will give you the XPATH of the element you seek.


*I got this bookmarklets from some other site but can't remember which one. Credit goes to the guy who created these JS.

18 Comments:

Anonymous said...

Works like a charm! Thank you for sharing!

Anonymous said...

I found the code posted on Stack Overflow at:
http://stackoverflow.com/questions/214862/equivalent-of-firebugs-copy-xpath-in-internet-explorer

The author is Phil Lho. http://stackoverflow.com/users/15459/philho

Anonymous said...

The person who wrote the code as well the person who shared it, a hearty thanks to both of them!!!!!!

Anonymous said...

For all those people who comments as,

"I found the code posted on Stack Overflow at:"

How many people did you share the information you found online. What to do who had written, bottom line is you found what you need.

Anonymous said...

Its really helpful to find Xpath value of a specific field..

Thanks to prepared and Shared information.

Anonymous said...

Thanks a tonn!!!

mathew said...

Thanks Man!!! It works. Also it there any way that i can verify some xpath?
Like the xpath which i get from Firefox or Chrome

Paras said...

Thanks Bro, It working.
Finally I know why my testcases was failing on IE.

Anonymous said...

Thanks Man!!!!!!!!Please keep up this good work......

man9ar00 said...

If you then need to test your XPath, you can use this technique:

http://autumnator.wordpress.com/2013/05/02/testing-xpath-and-css-locators-firepath-style-across-browsers/

Anonymous said...

This is awesome...Thanks for posting this... :)

Anonymous said...

Very Nice,

this is amazing :).

Thanks for you all

Ravi Kiran said...

very very helpfull. I just tried it and working perfectly.
Thanks

Anonymous said...

Hi Mahesh,

I did everything as mentioned in the post but not getting 'allow access button' or such things.

Can you please guide me on this.

Regards,
Istekhar

MD.M Rahman said...

This is nice But if you like to find out some tree menu you can't select this little "+" sign to expand.For noncritical element this is good way.

Anonymous said...

hi
it is helpful but i cant able to get xpath of the selected element in IE though i added getxpath1,2 to favourites sucessfully. i changed the security settings accrodingly but i cant able to retreive the xpath. pleae provide proper explanation. Thanks in advance.

pramod said...

thanks a lot .really helped me to solve my issue in IE

SANTHOSH KUMAR KORADA said...

Hi All,
I felt very happy when I got this solution but it is not working for me in my application. I have followed the steps given in the blog and fetch the xpath of an webelement which is not at all working and equals to the xpath which has fetched by using chrome browser or firebug is working fine.
For example, below are the x-paths for a button in one the page and both are different.
1. //*[@id="save"] xpath fetched as per the steps given in blog is not working for me.
2. //INPUT[@id='save'] xpath fetched fropm chrome browser which is working fine.

Everyone who read this blog has given positive comments but not fine in my case. So, can any one of you help me out in this regard?