我正在使用 NightmareJS 进行 headless 浏览。我的代码如下所示:
var Nightmare = require('nightmare');
var google = new Nightmare()
.goto('http://www.google.com')
.wait(3000)
.inject('js', 'jquery.min.js')
.screenshot('screenshot.png')
.evaluate(function(){
return $('#footer').html();
}, function(value){
console.log(value);
})
.run(function(err){
console.log('All done!');
});
我需要经常使用 console.log
调试 DOM 元素。但是,console.log 似乎无法在 .evaluate block 内工作。
如何将 .evaluate 中的内容记录到控制台?
最佳答案
所以我能够更早地使用 Promises 解决这个问题。这是更新后的代码:
var Nightmare = require('nightmare');
var Promise = require('es6-promise').Promise;
var nightmare = new Nightmare();
Promise.resolve(nightmare
.goto('http://www.google.com')
.wait(3000)
.inject('js', 'jquery.min.js')
.screenshot('screenshot.png')
.evaluate(function(){
return $('#footer').html();
}))
.then(function(value){
console.log(value);
console.log('All Done!');
return nightmare.end();
})
.then(function(result){
}, function(err){
console.error(err);
});
记住npm install es6-promise
。除了我在这里使用的之外,您还可以使用其他 Javascript Promises 实现。
希望有帮助。
关于javascript - 在 Nightmare 中将 DOM 元素写入浏览器控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34365427/