Htmlunit Doesn't Wait for JavaScript

HTMLUnit doesn't wait for Javascript

Thank you for responding.
I actually should have reported this sooner that I have found the solution myself.
Apparently when initialising WebClient with FF:

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);

It seem to be working.
When initialising WebClient with the default constructor it uses IE7 by default and I guess FF has better support for Ajax and is the recommended emulator to use.

HTMLUnit not return completely loaded page with JavaScript

Given the Problems HTMLUnit has with JavaScript, you need to find a workaround. Seeing that you know which element you want, you can implement a while loop. This could look somehow like this:

while(!page.asText().contains(„<div id=\„exmaple-id\">“)){
webClient.waitForBackgroundJavaScript(500);
}

If you are afraid of being catched in this loop, you could add a counting variable to the while condition. As far as my exeprience goes, is this a reliable way of dealing with this kind of delay.

HTMLUnit doesn't wait for Javascript 2

I think you're overcomplicating this. Additionally, I wouldn't recommend using the waitForBackgroundJavaScript method either. Furthermore, the HTMLUnit API itself doesn't recommend it.

I've explained in this question a better and simpler way to work with AJAX:

Get the changed HTML content after it's updated by Javascript? (htmlunit)

In Java and HtmlUnit, how to wait for a resulting page to finish loading and download it as HTML?

How do I programatically download the resulting web page as an HTML file

Try asXml(). Something like:

page = submit.click();
String htmlContent = page.asXml();
File htmlFile = new File("C:/index.html");
PrintWriter pw = new PrintWriter(htmlFile, true);
pw.print(htmlContent);
pw.close();


Related Topics



Leave a reply



Submit