简体   繁体   中英

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM