selenium - Equivalent of Firebug's "Copy XPath" in Internet Explorer? -
i have internet explorer web application.
i'm exploring can automate testing.
selenium looks tool, able activate links etc. need tell are. application wasn't built kind of testing in mind, there aren't id
attributes on key elements.
no problem, think, can use xpath expressions. finding correct xpath for, say, button, royal pain if done inspecting source of page.
with firefox / firebug, can select element use "copy xpath" expression.
i have ie developer toolbar , it's frustratingly close. can click select element of interest , display sorts of information it. can't see convenient way of determining xpath it.
so there way of doing ie?
i use bookmarklets. have 1 xpath related, don't know if works in ie. gotta go test , give if works on ie.
two bookmarklet sites web developers bookmarks: subsimple's bookmarklets , squarefree's bookmarklets. lot of useful things there...
[edit] ok, back. bookmarklet had ff only, , wasn't optimal. rewrote it, although using ideas original one. can't find found it.
expanded js:
function getnode(node) { var nodeexpr = node.tagname; if (nodeexpr == null) // eg. node = #text return null; if (node.id != '') { nodeexpr += "[@id='" + node.id + "']"; // don't need go //html, since ids supposed // unique, starting point. return "/" + nodeexpr; } // don't need //~ if (node.classname != '') //~ { //~ nodeexpr += "[@class='" + node.classname + "']"; //~ } // find rank of node among type in parent var rank = 1; var ps = node.previoussibling; while (ps != null) { if (ps.tagname == node.tagname) { rank++; } ps = ps.previoussibling; } if (rank > 1) { nodeexpr += '[' + rank + ']'; } else { // first node of kind @ level. there others? var ns = node.nextsibling; while (ns != null) { if (ns.tagname == node.tagname) { // yes, mark being first 1 nodeexpr += '[1]'; break; } ns = ns.nextsibling; } } return nodeexpr; } var currentnode; // standard (?) if (window.getselection != undefined) currentnode = window.getselection().anchornode; // ie (if no selection, that's body) else currentnode = document.selection.createrange().parentelement(); if (currentnode == null) { alert("no selection"); return; } var path = []; // walk dom while (currentnode != undefined) { var pe = getnode(currentnode); if (pe != null) { path.push(pe); if (pe.indexof('@id') != -1) break; // found id, no need go upper, absolute path ok } currentnode = currentnode.parentnode; } var xpath = "/" + path.reverse().join('/'); alert(xpath); // copy clipboard // ie if (window.clipboarddata) clipboarddata.setdata("text", xpath); // ff's code handle clipboard more complex // , might need change prefs allow changing clipboard content. // omit here isn't part of original request.
you have select element , activate bookmarklet xpath.
now, bookmarklet versions (thanks bookmarklet builder):
ie
(i had break in 2 parts, because ie doesn't long bookmarklets (max size varies depending on ie versions!). have activate first 1 (function def) second one. tested ie6.)
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;} 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();
ff
javascript:function o__o(){function getnode(node){var nodeexpr=node.tagname;if(nodeexpr==null)return null;if(node.id!=''){nodeexpr+="[@id='"+node.id+"']";return "/"+nodeexpr;}var rank=1;var ps=node.previoussibling;while(ps!=null){if(ps.tagname==node.tagname){rank++;}ps=ps.previoussibling;}if(rank>1){nodeexpr+='['+rank+']';}else{var ns=node.nextsibling;while(ns!=null){if(ns.tagname==node.tagname){nodeexpr+='[1]';break;}ns=ns.nextsibling;}}return nodeexpr;}var currentnode=window.getselection().anchornode;if(currentnode==null){alert("no selection");return;}var path=[];while(currentnode!=undefined){var pe=getnode(currentnode);if(pe!=null){path.push(pe);if(pe.indexof('@id')!=-1)break;}currentnode=currentnode.parentnode;}var xpath="/"+path.reverse().join('/');alert(xpath);}o__o();