Skip to content

Instantly share code, notes, and snippets.

@navchandar
Created October 28, 2019 10:29
Show Gist options
  • Save navchandar/c6d484dec29b838611f0e0bafab84e54 to your computer and use it in GitHub Desktop.
Save navchandar/c6d484dec29b838611f0e0bafab84e54 to your computer and use it in GitHub Desktop.
Detect Elements by XPATH using pure JS
function getElementsByXPath(xpath, parent) {
let results=[];
let query = document.evaluate(xpath, parent||document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for(let i = 0,length = query.snapshotLength;i < length;++i){
results.push(query.snapshotItem(i));
}
return results
};
items = getElementsByXPath("//*");
items.length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment