Skip to content
Advertisement

how to get the value from an xpath

I try to get the value from a XPath, but I always get “undefined” “null” as answer

that’s the HTML code from the XPath:

<div id="HZiTMlXgXNBTLizpJUukKkfUJGAbZlYt" style="display:inline;">20</div>

and I tried it multiple times with:

var test1 = document.evaluate('/html/body/div[1]/section/div/div/div/div/div/div[2]/div[1]/h2/span/div', document, null, XPathResult.ANY_TYPE, null).iterateNext().value;

var test2 = document.evaluate('/html/body/div[1]/section/div/div/div/div/div/div[2]/div[1]/h2/span/div', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

does someone have any idea?

Advertisement

Answer

You can get value by using:

xpath = '/html/body/.../some_element';
val = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value;

But

DIVs do not have a value property.

So you need to use another element with value property

Or use innerHTML to get 20 in your case:

val = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement