Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluate in PhantomJS doesn't seem to work

I have a problem with JavaScript magic. When I execute this code:

var page = require('webpage').create();
var url="http://google.com";
page.open(url, function (status){
  if (status!== "success") {
    console.log("Fail to load: "+url)
  }else{
    console.log('1');
    page.evaluate(function() {
      console.log('2');
      });   
    console.log('3');
  }
phantom.exit();
});

console have only 1 and 3 and no 2. Can anyone say why?

If I paste after my code DOM manipulation example (but it never execute) I have my two. Did I forget something important?

like image 882
Torv Avatar asked Dec 02 '22 22:12

Torv


2 Answers

PhantomJS won't log console messages in .evaluate() statements by default. Just include

page.onConsoleMessage = function (msg) {
    console.log(msg);
};

See this page for more details/in-depth example:

http://code.google.com/p/phantomjs/wiki/QuickStart#Code_Evaluation

like image 81
SomeKittens Avatar answered Dec 10 '22 13:12

SomeKittens


From Google Code

Any console message from a web page, including from the code inside evaluate(), will not be displayed by default. To override this behavior, use the onConsoleMessage callback.

like image 32
Widor Avatar answered Dec 10 '22 12:12

Widor