问题描述
我正在尝试使用PhantomJS(版本1.9.2)打开本地HTML文件:
I am trying to open a local HTML-file with PhantomJS (version 1.9.2):
var page = require('webpage').create(), fs = require('fs'),
address = "/Full/Path/To/test.html";
console.log('isFile? ' + fs.isFile(address));
console.log('isReadable? ' + fs.isReadable(address));
page.open(address, function(status){
console.log('status? ' + status);
console.log(page.content)
phantom.exit();
});
首先,我使用fs.isFile()
和amp;检查是否获得正确的路径以及文件是否可读. fs.isReadable()
.然后,我检查phantomjs是否成功打开文件(使用status
).与文件的实际内容无关,我总是得到:
First I check if I got the right path and if the file is readable using fs.isFile()
& fs.isReadable()
. Then I check whether phantomjs succeeded in opening the file (with status
). Independent of the actual contents of the file I always get:
isFile? true
isReadable? true
status? fail
<html><head></head><body></body></html>
因此文件和路径似乎都不错–但是PhantomJS无法打开它!有什么建议吗?
So the file and the path seem to be okay – but PhantomJS fails to open it!Any suggestions?
推荐答案
PhantomJS可以打开本地文件而没有任何问题.该URL必须遵循经典的Url/Uri规则,尤其是对于本地文件.
PhantomJS can open local files without any problems. The url have to follow classic Url/Uri rules, especially for a local file.
/Full/Path/To/test.html
对PhantomJS无效.是本地文件还是网络资源?
/Full/Path/To/test.html
is not valid for PhantomJS. Is it a local file or a web resource?
根据路径,只需尝试以下操作即可:
Depending of the path, just try with something like this:
file:///C:/Full/Path/To/test.html
或者如果它托管在Web服务器中:
or if it's hosted in a web server:
http://localhost/Full/Path/To/test.html
这篇关于PhantomJS无法打开本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!