我在这里修改了基本的 phantomjs 示例 http://phantomjs.org/screen-capture.html接受命令行参数。
当我通过 http://google.com作为参数 console.log 输出是正确的
0: index.js
但我的文件夹中没有任何 thumbnail.png 为什么?
var page = require('webpage').create();
var system = require('system');
var args = system.args;
var url;
if (args.length === 1) {
url = 'http://github.com/';
} else {
args.forEach(function(arg, i) {
console.log(i + ': ' + arg);
if (i > 0) {
page.open(arg, function() {
page.render('thumbnail' + '.png');
});
}
});
}
phantom.exit();
最佳答案
page.open
是一个异步函数,因此 phantom.exit
在您的回调呈现缩略图之前被调用。
按照文档中的说明将 phantom.exit
移到回调中
var page = require('webpage').create();
page.open('http://github.com/', function() {
page.render('github.png');
phantom.exit();
});
关于javascript - 为什么我的 phantomjs 脚本输出没有截图(命令行 args 版本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39369970/