console.log在CasperJS'使用setTimeout进行评估时不起作用 [英] console.log doesn't work in CasperJS' evaluate with setTimeout

查看:128
本文介绍了console.log在CasperJS'使用setTimeout进行评估时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么当我在 evaluate 中使用 console.log 时,它可以工作:

Why when I use console.log in evaluate, it works:

casper.then(function() {
  this.evaluate( function() {
    console.log('hello'); 
  });
});

但这不起作用:

casper.then(function() {
  this.evaluate( function() {
    setTimeout( function() {console.log('hello');}, 1000);
  });
});


推荐答案

因为你混淆了casperjs和远程页面环境。 evaluate 函数将执行远程页面env中的代码,因此 console.log 调用不会输出任何内容。

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.

如果你想抓住远程 console.log 来电,请听 remote.message 事件:

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.

这篇关于console.log在CasperJS'使用setTimeout进行评估时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆