嘿,我正在尝试制作一个 Nightmare 般的元素的屏幕截图,然后我想使用功能从此屏幕截图中提取文本,并在nightmare.type()
中使用此结果。问题是当我使用nightmare.run()
时,我收到一个错误,指出该屏幕截图不存在。我怎样才能解决这个问题?这是我的代码:
var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');
var nightmare = new Nightmare();
nightmare.goto('website');
//Takes screenshot of element
nightmare.use(Screenshot.screenshotSelector('screenshot' + i + '.png', 'img[id="yw0"]'));
nightmare.type('input[id=AuthForm_login]', username);
nightmare.type('input[id=AuthForm_password]', password);
nightmare.type('input[id=upload]', image.decodeFile('screenshot' + i + '.png', function(err, result){
//Returns Text from the Screenshot taken above!
return result.text;
}));
nightmare.run();
当我删除行时:
nightmare.type('input[id=upload]', image.decodeFile('screenshot' + i + '.png', function(err, result){
//Returns Text from the Screenshot taken above!
return result.text;
}));
一切正常,我得到了屏幕截图。
最佳答案
当 Nightmare 使用Phantomjs时,您可以那样做,尽管我发现下面的内容应该有所帮助。
归功于@fernandopasik,他说:
I found a way to do this, with the "use" method for executing plugins.
nightmare = new Nightmare(); urls.forEach(function (url) { nightmare.goto(url).screenshot(path).use(function () { console.log('finished one'); }); }); nightmare.run(function () { console.log('finished all'); });
NightmareJS screenshot callback
关于javascript - 使用所拍摄屏幕截图的功能的 Nightmare 类型结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28587266/