2013-03-19 113 views
0

我試圖使用HTMLUnit獲取網頁上的JavaScript元素(https://www.coursera.org/courses),並且它僅加載html數據。我如何獲得它顯示在javascript容器中顯示的信息?使用HTMLUnit獲取JavaScript元素

謝謝!

我當前的代碼:

 public String DownloadPage(String str){ 
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6); 
    webClient.getOptions().setTimeout(20000); 
    webClient.getOptions().setJavaScriptEnabled(true); 
    webClient.getOptions().setThrowExceptionOnScriptError(false); 

    try{ 
     HtmlPage page = webClient.getPage(str); 
     XmlPage page2 = webClient.getPage(str); 
     int n = webClient.waitForBackgroundJavaScript(100000); 

     System.out.println("Executing " + n + " JavaSript jobs!"); 
     System.out.println("OUTPUT: " + page2); 

     System.out.println("OUTPUT: " + page.asXml()); 
     webClient.closeAllWindows(); 
    } 

    catch(IOException e){ 
     JOptionPane.showMessageDialog(null, "error"); 
    } 


    webClient.closeAllWindows(); 
    return ""; 
} 

回答

0

使用

String theContent1 = webClient.getPage(theURL).getWebResponse().getContentAsString(); 

代替

String theContent2 = webClient.getPage(theURL); 

theContent1應包含實際頁面的源代碼,包括Java腳本(如果有的話)。