Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTMLUnit doesn't wait for Javascript

Tags:

I have a GWT based page that I would like to create an HTML snapshot for it using HtmlUnit. The page loads using Ajax/JavaScript information on a product, so for about 1 second there is a Loading... message and then the content appears.

The problem is that HtmlUnit doesn't seem to capture the information and all I'm getting is the "Loading..." span.

Below is an experimental code with HtmlUnit where I try to give it enough time to wait for the loading of the data but it doesn't seem to change anything and I am still unable to capture the data loaded by the GWT javascript.

        WebClient webClient = new WebClient();         webClient.setJavaScriptEnabled(true);         webClient.setThrowExceptionOnScriptError(false);         webClient.setAjaxController(new NicelyResynchronizingAjaxController());           WebRequest request = new WebRequest(new URL("<my_url>"));         HtmlPage page = webClient.getPage(request);          int i = webClient.waitForBackgroundJavaScript(1000);          while (i > 0)         {             i = webClient.waitForBackgroundJavaScript(1000);              if (i == 0)             {                 break;             }             synchronized (page)              {                 System.out.println("wait");                 page.wait(500);             }         }          webClient.getAjaxController().processSynchron(page, request, false);          System.out.println(page.asXml()); 

Any ideas...?

like image 471
Joly Avatar asked Apr 05 '11 16:04

Joly


1 Answers

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.

like image 138
Joly Avatar answered Oct 13 '22 09:10

Joly