I have a problem with JavaScript magic. When I execute this code:
var page = require('webpage').create();
var url="";
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?
I have a problem with JavaScript magic. When I execute this code:
var page = require('webpage').create();
var url="http://google.";
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?
Share Improve this question edited Jan 5, 2015 at 23:16 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Jun 20, 2012 at 14:24 TorvTorv 1,2342 gold badges12 silver badges29 bronze badges3 Answers
Reset to default 13PhantomJS 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./p/phantomjs/wiki/QuickStart#Code_Evaluation
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.
If you only want select logs to e through, you can return the value that you'd like to print.
For example:
console.log(page.evaluate(function() {
return '2';
});