簡體   English   中英

在java程序中執行phantomjs腳本

[英]Execute phantomjs script in java program

我有一個問題:我必須在java程序中執行一個phantomjs腳本,但我不能。 這是腳本的代碼:

var page = require('webpage').create();
page.open('http://stackoverflow.com',function(status){
if(status !== 'success'){
    console.log('Open failed');
}else{
   console.log(page.evaluate(function(){
        return document.documentElement.outerHTML;
   }));
} 
phantom.exit();
});

在實踐中,我想執行網頁的javascript並在獲取html結果之后。 我嘗試使用javascript,JSP或Java上運行Phantomjs的鏈接中的代碼,但我的程序在此行被阻止

int exitStatus = process.waitFor();

我該怎么做才能解決這個問題?

謝謝

您可以使用Runtime類來執行終端命令。 我能夠使用以下程序生成我想要的輸出。

Runtime rt = Runtime.getRuntime();
    try{
        Process pr = rt.exec("pathToPhantomHome/phantomjs "+ " pathToJSfile");
        BufferedInputStream bis = new BufferedInputStream(pr.getInputStream());
        bis.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM