Question

I am working with java to get some information from a web page. The problem is the information I need is generated by a JavaScript function. How can get this information because the code below brings only page information before full loaded (which means I can get only frames of the page).

code1.

URL target = new URL()
HttpURLConnection con = (HttpURLConnection)target.openConnection();
StringBuffer sb = new StringBuffer();
String line = "";
BufferedReader br = null

try {
    br = new BufferedReader(new InputStreamReader(con.getInputStream()));

    while((line = br.readLine()) != null){
        sb.append(line);
    }
} catch(Exception e){
    e.printStackTrace();
}

Is there a way to know when the page has fully loaded in java? (Extra library can be an answer, but I wish to do it in java only). Thanks.

Was it helpful?

Solution

Your are making an HTTP request from java, this returns a text stream, the concept of "page loaded" is a browser related concept, the browser requests the content of the page (same as you are doing) and then renders the page and executes Javascript. It's the browser that executes Javascript.

If you want to make this only in Java, you need to implement a headless browser (a browser without user interface), or at least get the Javascript in the page you are loading and executing this. Doing this from scratch in pure Java is not an easy task, check out HtmlUnit for an example.

OTHER TIPS

Java won't execute any client-side JavaScript. It will just read it. If you want a browser, use a browser.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top