0

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
    }
});
1
  • Look at the documentation again... page.evaluate takes two parameters, both functions. Commented Dec 21, 2015 at 15:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.