Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to use HTMLUnit to get the javascript elements on a webpage (https://www.coursera.org/courses), and it is only loading the html data. How do I get it to display the information shown in the javascript container?

Thanks!

My current code:

     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 "";
}
share|improve this question
add comment

1 Answer

use

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

instead of

String theContent2 = webClient.getPage(theURL);

theContent1 should contain the actual page source including JavaScripts (if any).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.