PhantomJS 2.0.0 page.open() 不再适用于本地文件
PhantomJS 2.0.0 page.open() is no longer working for local file
我有以下名为 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]);
当我在 windows 下使用 PhantomJS 1.9.8 和本地 html 文件时,我得到了正确的输出。
phantomjs.exe render.js C:\test.htm
当我对 PhantomJS 2.0.0 使用完全相同的命令时,我得到一个空白页面。可能是 PhantomJS 2.0.0 中的错误?
PhantomJS 2 需要具有协议的 URL,它错误地将 C:
识别为有效协议,例如 file:
或 http:
。所以你应该使用
page.open("file:///"+system.args[1]);
告诉 PhantomJS 这应该是一个本地文件。
有效的"URLs"是:
file:///C:/test.htm
test.htm
同一文件夹中的文件
无效 "URLs" 是:
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();
}
});
我有以下名为 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]);
当我在 windows 下使用 PhantomJS 1.9.8 和本地 html 文件时,我得到了正确的输出。
phantomjs.exe render.js C:\test.htm
当我对 PhantomJS 2.0.0 使用完全相同的命令时,我得到一个空白页面。可能是 PhantomJS 2.0.0 中的错误?
PhantomJS 2 需要具有协议的 URL,它错误地将 C:
识别为有效协议,例如 file:
或 http:
。所以你应该使用
page.open("file:///"+system.args[1]);
告诉 PhantomJS 这应该是一个本地文件。
有效的"URLs"是:
file:///C:/test.htm
test.htm
同一文件夹中的文件
无效 "URLs" 是:
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();
}
});