There is no validation code in your example. Let's see what we can do here.
PhantomJS has two contexts. page.evaluate()
is a sandboxed function that provides access to the DOM context (or page context). The problem is that you cannot return DOM nodes from the DOM context, because only primitive values can be passed.
If you want to check if an element is available in the page, you can do this:
page.open('file://' + __dirname + '/content.txt', function (status) {
page.evaluate(function _inDomContext() {
return !!document.getElementById('some_id');
}, function _inOuterContext(result){
console.log("result: " + result); // true or false
ph.exit();
});
});
Here are some things that you should notice:
!!
evaluates the value of the following expression to a boolean which can be passed out of the DOM context.
- phantomjs-node is a bridge between node.js and PhantomJS which transforms every function call that is synchronous in PhantomJS to an asynchronous call in phantomjs-node (additional callback).
ph.exit()
should be called at the end. Don't forget to consider the asynchronous behavior and potential callbacks.
If you want to wait for a specific condition (e.g. some element is loaded asynchronously) in phantomjs-node, then you can use the function in this answer.