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
How to Reference a Method in Javadoc
Environment Variable with Maven
What to Return If Spring MVC Controller Method Doesn't Return Value
How to Simulate a Real Mouse Click Using Java
Displaying Fancy Equations with Java
Making a Log4J Console Appender Use Different Colors for Different Threads
What Is an "Internal Address" in Java
Java Sorting Based on Enum Constants
Any Good Recommendations for Mp3/Sound Libraries for Java
Can a Class Have No Constructor
Is -Djava.Library.Path=... Equivalent to System.Setproperty("Java.Library.Path", ...)
Does Java Se 8 Have Pairs or Tuples
Java 8 Stream with Batch Processing
Annotation to Make a Private Method Public Only for Test Classes
How to Solve the "A Generic Array of T Is Created for a Varargs Parameter" Compiler Warning