使用casper/phantomjs输出客户端控制台

20

查看CasperJS文档,我找不到能够查看客户端JavaScript的console.log的地方。这个是否可能?

1个回答

44

我不是很确定是否完全理解你的问题,但是你可以像以下这样做:

var casper = require('casper').create({
    logLevel: "debug"
});

casper.on('remote.message', function(message) {
    this.echo(message);
});

casper.start('http://google.com/', function() {
    this.evaluate(function sendLog(log) {
        // you can access the log from page DOM
        console.log('from the browser, I can tell you there are ' + log.length + ' entries in the log');
    }, this.result.log);
});

casper.run();

输出:

$ casperjs log.js 
from the browser, I can tell you there are 4 entries

对我来说,我不认为“从浏览器” - Abhik Bose Pramanik
1
当我使用this.echo时,它对我有效,我已编辑了你的答案。 - jgillich
旧的答案,但是如果想要在浏览器中看到类似from the browser这样的内容,只需将其添加到函数的this.message部分中,例如:this.echo("from the browser, " + message); - Jeff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接