0
var phantom = require('phantom');

phantom.create()
        .then(function (ph) {
            _ph = ph;
            return ph.createPage();
        })
        .then(function(page) {
            _page = page;
            url = "http://www.aeiou.pt";
            return page.open(url);
        })
        .then(function(page) {

        console.log("hello3");
            page.evaluate(function () {

My code starts with something like this. The console.log "hello3" is printed but then, it gives me error:

TypeError: page.evaluate is not a function at /home/someone/server123.js:58:11 at at process._tickCallback (internal/process/next_tick.js:188:7)

Why it happens in this situation?

Node version: v8.6.0

Npm version: 5.3.0

Phantom version: phantom@4.0.5

PRVS
  • 1,612
  • 4
  • 38
  • 75

1 Answers1

2

The problem you are having is that page.open() does not return the page -- it returns the status. So the value being passed to the next then() is the status, which you try to call evaluate on that. This, of course, doesn't work.

The way they handle this in their example is have a page variable outside the then() chain that they can access inside each then(). You are almost doing that with _page = page; If _page is defined outside the function, you should be able to call _page.evaluate() rather than calling it on the return value from open().

var phantom = require('phantom');
var _page;

phantom.create()
    .then(function (ph) {
        _ph = ph;
        return ph.createPage();
    })
    .then(function(page) {
        _page = page;
        url = "http://www.aeiou.pt";
        return page.open(url);
    })
    .then(function(status) {
        // check status for errors here
        console.log("hello3");
        _page.evaluate(function () {
Mark
  • 90,562
  • 7
  • 108
  • 148
  • Yes, that's it! Thank's! – PRVS Oct 19 '17 at 16:34
  • my question is solved but now, inside _page.evaluate(function() { I can't print anything, it seems that it there are any error... I tried to throw an error but without success. Any idea why? – PRVS Oct 19 '17 at 16:43
  • Yeah, that's something that's a little tricky - the code you run in `evaluate()` is running in the context of the page you are evaluating. There's a good thread on the issue here: https://stackoverflow.com/questions/16701208/phantomjs-page-evaluate-not-logging-onto-console – Mark Oct 19 '17 at 17:06
  • @Mark_M `page.set('paperSize', {format: 'A4', orientation: 'portrait'});` `page.set` is not a function any idea ? – Gunjan Patel Feb 08 '18 at 10:34