Reputation: 4048
I use this nodejs module for parsing HTML. I'm trying to find element by id in my HTML. And I can't get document object. Here is my code:
var phantom = require('phantom');
var fs = require('fs');
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open('file://' + __dirname + '/content.txt', function (status) {
page.evaluate(function() {
// this code will never executed
document.getElementById('some_id');
});
ph.exit();
});
});
}, {
dnodeOpts: {
weak: false
}
});
Upvotes: 0
Views: 402
Reputation: 61892
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.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.
Upvotes: 2