[英]Logging in casper.js if console.log disable
以下使用casper.js
代码不输出This is thenEvaluate
string 因为Twitter.com禁用了console.log
(空函数):
var casper = require('casper').create({
verbose: false,
logLevel: 'debug'
});
casper.start("http://twitter.com");
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
})
casper.thenEvaluate(function() {
console.log('This is thenEvaluate');
});
casper.run();
如果我将url交换到google.com或任何其他网站,它就可以工作。 我的问题是:
对于网站禁用console.log
,有没有办法重新启用它?
如果 #1 为 NO,有没有办法在evaluate()
或thenEvaluate()
函数中做任何类型的日志?
谢谢。
我在这里找到了关于恢复 console.log()的答案
var i = document.createElement('iframe');
i.style.display = 'none';
document.body.appendChild(i);
window.console = i.contentWindow.console;
只需删除console
对象的log
属性:
>>> console.log('plop')
undefined
>>> delete console.log
true
>>> console.log('plop')
plop
undefined
这可以给 casper:
var casper = require('casper').create();
casper.start("http://twitter.com");
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
})
casper.thenEvaluate(function() {
delete console.log;
console.log('This is thenEvaluate');
});
casper.run();
免责声明:答案是在这里找到的,应相应地注明原作者。
您可能需要考虑构建一个断言小部件,它将一种可视化的 console.log 效果附加到 DOM,然后您可以输出文本。 我在 Github 上有一个 Gist,我已经在这样的情况下实现了类似的东西: https : //gist.github.com/3773871
如果您只是将该代码调用到您的程序中,则可以使用
assert(true, 'This is thenEvaluate()')
显然,您并没有真正使用它作为正确的断言,但是如果您只是传入 true 和一个字符串并调用它来代替您的控制台日志,它会将您的结果输出到屏幕右上角的绝对定位元素用于阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.