Tip to Find XPath in IE Browser
We often encounter a need to automate our application in IE browser. This may turn out a bit tricky as IE does not have tool or add-on like Firebug. You will have to write XPaths on your own. Or you can use this simple tip to find XPath in IE. This tip uses JavaScript to build IE bookmarklet for fetching an element XPath.
Follow below steps to find XPath in IE browser.
- Open IE.
- Type about:blank in address bar and hit Enter key.
- Bookmark current page (Using Favorites –> Add to Favorites option or using CTRL+D keyboard shortcut).
- Expand Favorites menu and locate your saved bookmark.
- Right click on the saved bookmark and select Properties option.
- Select Web Document tab from Properties dialog box and add below JavaScript code in URL box.
1234567891011121314151617181920212223242526272829303132333435363738javascript: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;} - Save the bookmark.
- Perform steps 2-4.
- Add below JavaScript code in URL box of this newly created bookmark and save it.
123456789101112131415161718javascript:function GetXPATH_final(){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);}GetXPATH_final(); - Now, select an element(Eg. link, button, image, checkbox, text etc) for which you want to find XPath. Here select means clicking the left mouse button just before the element begins and dragging it till the element ends.
- Select the first bookmark and second bookmark consecutively. Upon performing this step, element XPath gets copied to clipboard.
- You can then paste the XPath in any editor (Eg. Notepad).
This is not a sure shot way to find XPath in IE browser. It has limitations in selecting web elements such as drop down values, hidden elements etc. However, it comes in handy for basic use. For more thorough automation, you should derive XPath yourself.
Comments
Can you a video of this creation….
Useless 🙁 Useless 🙁 Useless 🙁 Useless 🙁 Useless 🙁 Useless 🙁 Useless 🙁 Useless 🙁 Useless 🙁 Useless 🙁
here has a tool : https://github.com/xwjie/IEXPath
not working in IE11
Not working in IE11
NOt working in IE 11