codecowboy
codecowboy

Reputation: 10095

Why does this code cause Node.js using the phantom module to hang

If I change this:

var phantom = require('phantom');
phantom.create(function(ph) {
    return ph.createPage(function(page) {
        return page.open("http://www.google.com", function(status) {
            console.log("opened google? ", status);
            return page.evaluate((function() {
                return document.title;
            }), function(result) {
                console.log('Page title is ' + result);
                return ph.exit();
            });
        });
    });
});

to this:

 var phantom = require('phantom');
    phantom.create(function(ph) {
        return ph.createPage(function(page) {
            return page.open("http://www.google.com", function(status) {
                console.log("opened google? ", status);
                return page.get('title',(function(title) {
                    return title;
                }), function(result) {
                    console.log('Page title is ' + result);
                    return ph.exit();
                });
            });
        });
    });

Node hangs at the console after printing 'opened google? success'and there is no further output.

I am trying to use page.get() instead of page.evaluate as described in the phantom module docs:

Properties can't be get/set directly, instead use p.get('version', callback)

Upvotes: 2

Views: 253

Answers (1)

Paul Mougel
Paul Mougel

Reputation: 17038

You misuse page.get(). This method has only two arguments, not three.

Here's how to it:

page.get('title', function(title) {
  console.log('Page title is ' + title);
  return ph.exit();
});

Upvotes: 2

Related Questions