我有以下名为render.js的javascript文件:
var page = require('webpage').create(),
system = require('system');
page.onLoadFinished = function () {
console.log(page.content);
phantom.exit();
};
page.open(system.args[1]);
当我在带有本地html文件的Windows下使用PhantomJS 1.9.8时,我得到正确的输出。
phantomjs.exe render.js C:\test.htm
当我使用与PhantomJS 2.0.0完全相同的命令时,我得到一个空白页面。也许是PhantomJS 2.0.0中的一个错误?
答案 0 :(得分:3)
PhantomJS 2期望具有协议的网址错误地将C:
标识为有效的协议,例如file:
或http:
。所以你应该使用
page.open("file:///"+system.args[1]);
告诉PhantomJS这应该是一个本地文件。
有效"网址"是:
file:///C:/test.htm
test.htm
表示同一文件夹中的文件无效"网址"是:
C:/test.htm
file:///test.htm
表示同一文件夹中的文件最好检查status
值并注册onResourceError
之类的错误事件:
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.open(finish, function(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
phantom.exit(1);
} else {
console.log(page.content);
phantom.exit();
}
});