I built a function in Javascript without Jquery that creates a unique identifier for any element in the DOM. If it has an id it uses the id. Otherwise it creates a path based on the parents of that element:
function getSelector(node) {
var id = node.getAttribute('id');
if (id) {
return '#'+id;
}
var path = '';
while (node) {
var name = node.localName;
var parent = node.parentNode;
if (!parent) {
path = name + ' > ' + path;
continue;
}
if (node.getAttribute('id')) {
path = '#' + node.getAttribute('id') + ' > ' + path;
break;
}
var sameTagSiblings = [];
var children = parent.childNodes;
children = Array.prototype.slice.call(children);
children.forEach(function(child) {
if (child.localName == name) {
sameTagSiblings.push(child);
}
});
// if there are more than one children of that type use nth-of-type
if (sameTagSiblings.length > 1) {
var index = sameTagSiblings.indexOf(node);
name += ':nth-of-type(' + (index + 1) + ')';
}
if (path) {
path = name + ' > ' + path;
} else {
path = name;
}
node = parent;
}
return path;
}
So if you want to click on an element and get the unique selector just add:
window.addEventListener('click', function(event) {
var ele = document.elementFromPoint(event.x, event.y);
alert(getSelector(ele));
});