Why when I use console.log
in evaluate
, it works:
casper.then(function() { this.evaluate( function() { console.log('hello'); }); });
But this doesn't work:
casper.then(function() { this.evaluate( function() { setTimeout( function() {console.log('hello');}, 1000); }); });
Because you're mixing up casperjs and remote page environments. The evaluate
function will execute code within the remote page env, so the console.log
call won't output anything.
If you want to catch remote console.log
calls, listen to the remote.message
event:
casper.on('remote.message', function(msg) { this.echo('remote message caught: ' + msg); })
Btw, documentation for events is pretty much exhaustive, as well as the one for evaluate.
@NiKo's answer is critical.
I would also suggest adding the following, since if there's an error, you might not even make it far enough to print out a console.log() msg, and instead end up with silence.
casper.on( 'page.error', function (msg, trace) { this.echo( 'Error: ' + msg, 'ERROR' ); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With