2013-06-21 41 views

回答

1

PhantomJS需要一个DOM,并且不会出现在一个空文件中。您需要为您的页面,在页面上执行JavaScript至少创建这种结构:

<html> 
    <head> 
     <!-- JavaScript goes here !--> 
    </head> 
    <body> 
    </body> 
</html> 
1

我其实是在寻找这样的事情:

var page = require('webpage').create(); 
page.open('about:blank', function(status) { 
    page.evaluate(function() { 
     //My script here.   
    }); 
    phantom.exit(); 
}); 
+0

这应该工作,因为它加载了一个空白页e基本的HTML结构。 –

1

尝试page.setContent

http://phantomjs.org/api/webpage/method/set-content.html

var webPage = require('webpage'); 
var page = webPage.create(); 
var expectedContent = '<html><body><div>' + yourScript + '</div></body></html>'; 
var expectedLocation = 'http://www.phantomjs.org/'; 
page.setContent(expectedContent, expectedLocation, function(status) { 
    if(status === success) { 
     page.evaluate(); 
    } else { 
     //do something 
    } 
});