admin管理员组文章数量:1332890
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=".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?
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?
Share Improve this question asked Aug 31, 2012 at 14:55 uzil24uzil24 1564 silver badges14 bronze badges 4- 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 Commented Sep 7, 2012 at 8: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 pletely identical. – uzil24 Commented Sep 7, 2012 at 13:20
- what are you mean with selenium? this one? docs.seleniumhq/docs/… – user3434609 Commented Feb 28, 2013 at 18:20
- @user3434609 yes. I switched to Selenium 2 years ago and had almost no problems since. – uzil24 Commented Sep 8, 2014 at 6:39
2 Answers
Reset to default 4The most likely problem is that HtmlUnit isn't waiting for the JavaScript to finish running. The HtmlUnit FAQ suggests 3 workarounds: http://htmlunit.sourceforge/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.
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
});
});
本文标签: javaHtmlAnchor click() function in Htmlunit is not workingStack Overflow
版权声明:本文标题:java - HtmlAnchor click() function in Htmlunit is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742314521a2451548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论