javascript - console.log 在 CasperJS 的 setTimeout 评估中不起作用

标签 javascript casperjs

为什么当我在 evaluate 中使用 console.log 时,它有效:

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

但这行不通:

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

最佳答案

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

如果你想捕获remote console.log 调用,监听remote.message 事件:

casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
})

顺便说一句,documentation for events非常详尽,以及the one for evaluate .

关于javascript - console.log 在 CasperJS 的 setTimeout 评估中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11864373/

相关文章:

javascript - 如何使用 underscore.js 每个循环渲染 TREE JSON?

javascript - 如何使用vizceral graph(vizceral.js)在AngularJS中显示数据流?

javascript - 使用 javascript(或 jQuery)选择和操作 CSS 伪元素,例如::before 和::after

PHP 执行 casperjs/phantomjs 脚本

javascript - AngularJS中的服务和 Controller 之间没有发生数据绑定(bind)

php - 解析 JSON 数据时 Highcharts 数据显示问题

javascript - 使用 Casper/Phantom 读取 HTML 属性因环境而异

javascript - 当选择器未显示预期文本时该怎么办?

xpath - 如何使用CasperJS和XPath获取元素的HREF值

php - 有什么方法可以使用 PhantomJs/CasperJS 或类似的 headless 浏览器来记录 Action 吗?