i am trying to get the xpath of any element on the web page, i have tried and and got some answers here on stackoverflow, but the xpath generated is longer, it is correct but long. as in selenium ide it gives small unique xpath. is there any better function i could use to get the smallest unique xpath ?
i referred this link : I'm storing click coordinates in my db and then reloading them later and showing them on the site where the click happened, how do I make sure it loads in the same place?
this is the function i used :
function getPathTo(element) {
if (element.id!=='')
return "//*[@id='"+element.id+"']";
if (element===document.body)
return element.tagName.toLowerCase();
var ix= 0;
var siblings= element.parentNode.childNodes;
for (var i= 0; i<siblings.length; i++) {
var sibling= siblings[i];
if (sibling===element) return getPathTo(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix + 1) + ']';
if (sibling.nodeType===1 && sibling.tagName === element.tagName) {
ix++;
}
}
}