u2amea
Last Updated: February 25, 2016
·
16.99K
· andrewstilliard
Wfprofile

Javascript get element by xpath

Although its rare i'll use xpath, if you're ever testing it for web crawling etc you can just fire this in the browser console to log the element:

document.evaluate('XPATH HERE', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

So say you'd like to quickly grab some elements innerHTML:

document.evaluate('/html/body/div[4]/div[2]/div/div/div/div[3]/div/span[2]/span', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML;
Say Thanks
Respond