I'm trying to get XPath working with PhantomJS 1.9.2:
var getElementsByXPath = function(xPath) {
return document.evaluate(
xPath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
};
var root = getElementsByXPath("//div").iterateNext();
This is being executed upon page load and always returns null whereas querySelector seems to work correctly:
var divs = page.evaluate(function(s) {
return document.querySelector(s);
}, 'div');
Did I miss something in this particular XPath evaluate sample?
I have finally found out that the call document.evaluate must be embraced with a page.evaluate call like the following:
page.evaluate(function() {
document.evaluate(
'//div',
document,
null,
XPathResult.ORDERED_NODE_ITERATOR_TYPE,
null);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With