0

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++;
        }
    }
}
Community
  • 1
  • 1
Abhishek Tripathi
  • 1,211
  • 2
  • 15
  • 30
  • Why do you need shorter XPaths? Surely if it works, it works – Ben C Mar 01 '16 at 11:19
  • yes it works, but i want to generate smaller as selenium ide do, i am working on a chrome extension where i want this, i am just trying to get smaller xpath. if i can then i want to ttry for that too. – Abhishek Tripathi Mar 01 '16 at 11:24
  • why don't you use css selectors, css classes or IDs instead of xpath? – fabdurso Mar 01 '16 at 11:31
  • @fabersky i have already made one to get the css selector ! but as i said i want this for my extension thats why i want xpath as i need xpath also in my extension. – Abhishek Tripathi Mar 01 '16 at 11:58

0 Answers0