Closed cstayyab closed 3 years ago
Hi Muhammad,
The functionality you are looking for has very little to do with the purpose of the library. Your needs can easily be met with a simple function, see quick prototype in vanilla javascript:
function getXPathForElement(element) {
var path = [];
for (; element && element.nodeType != 9; element = element.parentNode) {
for (var sibling = element, index = 1; sibling = sibling.previousSibling;) {
if (sibling.nodeName == element.nodeName) {
index++;
}
}
path.unshift(element.localName + "[" + index + "]");
}
return '/' + path.join("/");
}
Can we get XPath of an element when nothing is passed in the
xpath()
just like when we don't pass any value toval()
we get its current value. I had something like this in mind: