2

空白のページを読み込んで、そのページで JavaScript を実行することはできますか? もしそうなら、どのように?例をいただければ幸いです。

4

3 に答える 3

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
   }
});
于 2016-03-05T00:18:39.303 に答える
1

私は実際に次のようなものを探していました:

 var page = require('webpage').create();
 page.open('about:blank', function(status) {
    page.evaluate(function() {
       //My script here.         
    });
    phantom.exit();
});
于 2013-06-21T19:26:11.427 に答える
1

PhantomJS には DOM が必要ですが、これは空のファイルには存在しません。ページで実行するには、少なくとも次の構造を作成する必要がありJavaScriptます。

<html>
    <head>
        <!-- JavaScript goes here !-->
    </head>
    <body>
    </body>
</html>
于 2013-06-21T18:23:50.210 に答える