![](/img/trans.png)
[英]Why console.log in puppeteer page.evaluate doesn't work?
[英]console.log doesn't work in CasperJS' evaluate with setTimeout
為什么當我在evaluate
使用console.log
時,它的工作原理是:
casper.then(function() {
this.evaluate( function() {
console.log('hello');
});
});
但這不起作用:
casper.then(function() {
this.evaluate( function() {
setTimeout( function() {console.log('hello');}, 1000);
});
});
@ NiKo的答案很關鍵。
我還建議添加以下內容,因為如果出現錯誤,您甚至可能無法打印出一個console.log()消息,而最終會沉默。
casper.on( 'page.error', function (msg, trace) {
this.echo( 'Error: ' + msg, 'ERROR' );
});
CasperJS包含ClientUtils ,可以從遠程頁面使用它輕松登錄到casper腳本的控制台:
__utils__.echo('This message is logged from the remote page to the CasperJS console');
在@ odigity的答案的基礎上,這使得casperjs死於更熟悉的錯誤/堆棧跟蹤:
var util = require('util');
casper.on('page.error', function exitWithError(msg, stack) {
stack = stack.reduce(function (accum, frame) {
return accum + util.format('\tat %s (%s:%d)\n',
frame.function || '<anonymous>',
frame.file,
frame.line
);
}, '');
this.die(['Client-side error', msg, stack].join('\n'), 1);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.