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:
JavaScript
x
2
1
<div id="HZiTMlXgXNBTLizpJUukKkfUJGAbZlYt" style="display:inline;">20</div>
2
and I tried it multiple times with:
JavaScript
1
4
1
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;
2
3
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;
4
does someone have any idea?
Advertisement
Answer
You can get value
by using:
JavaScript
1
3
1
xpath = '/html/body/.../some_element';
2
val = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value;
3
But
So you need to use another element with value
property
Or use innerHTML to get 20
in your case:
JavaScript
1
2
1
val = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML;
2