Skip to content Skip to sidebar Skip to footer

Error Executing Javascript Using Htmlunit

I am trying to execute a JavaScript on the following URL: http://steamcommunity.com/id/bobcatchris/inventory#730. I can do it using google chrome by opening console by pressing ctr

Solution 1:

These are your javascript script errors translated to Java exceptions by your framework.

That's because you have not explicitly set the setThrowExceptionOnScriptError option of your WebClient to false.

Unless there are absolutely no errors in the js, it is useful to always set this value as false unless it interferes with the results your are looking for.

Typically, these are my webclient settings when dealing with js and ajax through HtmlUnit.

finalWebClient webClient = new WebClient(BrowserVersion.FIREFOX_17,
                PROXY_HOST, PROXY_PORT);

WebRequest request = new WebRequest(new URL(
                "http://steamcommunity.com/id/bobcatchris/inventory#730"));

webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.setJavaScriptTimeout(10000);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setTimeout(10000);

HtmlPage page = webClient.getPage(request);

          String script="var list = [];\n"+"\n"+"\n"+"var size = Object.keys(g_ActiveInventory.rgInventory).size();\n"+"\n"+"\n"+"\n"+"var counter = 0;\n"+"\n"+"while (counter < size) {\n"+" list.push(g_ActiveInventory.rgInventory[Object.keys(g_ActiveInventory.rgInventory)[counter]].market_name);\n"+" counter +=1;\n"+"}";
            Object result = page.executeJavaScript(script).getJavaScriptResult();
            System.out.println(result);

If I try your code, with the above settings I get 150.0 printed to the console which I assume is working as expected.

EDIT:

To traverse over the complete array list:

String script="var list = [];\n"+"\n"+"\n"+"var size = Object.keys(g_ActiveInventory.rgInventory).size();\n"+"\n"+"\n"+"\n"+"var counter = 0;\n"+"\n"+"while (counter < size) {\n"+" list.push(g_ActiveInventory.rgInventory[Object.keys(g_ActiveInventory.rgInventory)[counter]].market_name);\n"+" counter +=1;\n"+"}"+"list";

            Object result = page.executeJavaScript(script).getJavaScriptResult();
            if (result instanceof NativeArray) {
                for (Object obj : (NativeArray)result) {
                    System.out.println(obj);    
                }

            }

Above, I have change the js to include list as the return parameter and iterate over NativeArray to get each element.

Output:

P2000 | Scorpion (Factory New)
AK-47 | Black Laminate (Field-Tested)
★ StatTrak™ Karambit | Case Hardened (Minimal Wear)
CS:GO CaseKeyCS:GO CaseKey

You can read much on their ajax settings at their FAQs - here.

Post a Comment for "Error Executing Javascript Using Htmlunit"