u2amea
Last Updated: April 12, 2018
·
25.17K
· 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

1 Response
Add your response

29812

I joined coderwall just to say thank you. I had been trying to use the JQuery $x() function to aquire an element and although it would return it, the browser was not allowing me to access the innerHTML and innerTEXT variables."

3 months ago ·