3

I am trying to use HtmlUnit for browsing automatically a site. I need to press some buttons in the process. First I build an HtmlAnchor object of a button with this xml:

<a href="dog.php">
  <img src="http://images.hand.co.uk/Pic/site_images/hand/Myper/MyOrder/images/DogRed.gif" width="75" height="31" border="0" alt="1 adds"/>
</a>

which works fine when I click it using the click() method. I am then moved to another page in which I have link on which I need to click for the desired contents to appear. After the click I am not moved to another page and it is merely a Java script function firing.

this is the anchor for the second link:

<a style="color: black; font-weight: bold;" href="javascript:show_me('DogDetails.php?DogID=2445485', 2445485, 800);">
  details
</a>

For both of those elements I am using the HtmlAnchor object with it's click() method. But that method is doing nothing at all for the second element.

I have also tried using the JavaScript Engine built in HtmlUnit, but had no success. how can I click this persistent link with the HtmlUnit platform?

uzil24
  • 156
  • 4
  • 13
  • Could you post some more details please? When you click() the javascript link, how do you know it isn't working? What do you expect to change on the page? – Neil Vass Sep 07 '12 at 08:49
  • After this click I expect the page to change. some new links and pictures should appear in the page. The situation is that the page before and after are completely identical. – uzil24 Sep 07 '12 at 13:20
  • what are you mean with selenium? this one? http://docs.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations – user3434609 Feb 28 '13 at 18:20
  • @user3434609 yes. I switched to Selenium 2 years ago and had almost no problems since. – uzil24 Sep 08 '14 at 06:39

2 Answers2

4

The most likely problem is that HtmlUnit isn't waiting for the JavaScript to finish running. The HtmlUnit FAQ suggests 3 workarounds: http://htmlunit.sourceforge.net/faq.html#AJAXDoesNotWork.

Of these, the neatest solution to try is to get your WebClient to wait for AJAX requests to finish:

webClient.setAjaxController(new NicelyResynchronizingAjaxController());

I have found that some sites do a clever trick of running AJAX on a background thread - this means the NicelyResynchronizingAjaxController won't wait for it to finish, as it only watches the main UI thread. There's a good answer here that explains how to wait for all threads rather than just the main one.

Community
  • 1
  • 1
Neil Vass
  • 5,251
  • 2
  • 22
  • 25
  • I tried all those methods. nothing seems to work. I followed the runtime code as much as I could understand. it seems that in the class ScriptRuntime, on line 3183: result = f.doTopCall(callable, cx, scope, thisObj, args); The execution initially fails since 'result' returns as undefined. I could not understand why... – uzil24 Sep 10 '12 at 20:22
  • To anyone visiting this question, I eventually solved my problem. The solution was switching to Selenium which has no problem clicking the mentioned link. – uzil24 Oct 20 '12 at 16:27
2

I've had a somewhat similar problem, tried several ways to wait for javascript to run in the background, but had no success.

I had half a mind to switch to Selenium, but it "solved itself" after disabling CSS on the WebClient:

WebClient.getOptions().setCssEnabled(false);

Whenever we reenable the CSS, the .click() just stops working.

My anchor was:

<div class="my-anchor's-parent-class"/>
  <a href="javascript:void(0) class="text" id="buttonSearch" style="display: block;">Search</a>
</div>

It had some JQuery attaching the .click() handler to it, who acted based on the 'class' property of my anchor's parent:

    $('.my-anchor's-parent-class').each(function () {
        $(this).children('a').click(function () {
          // if parent has another given class appended, call .myFunction(this)
          // else, call other function
        });
    });
vergatti
  • 166
  • 1
  • 6