Htmlunit中的HtmlAnchor click()函数不起作用 [英] HtmlAnchor click() function in Htmlunit is not working
问题描述
我正在尝试使用HtmlUnit自动浏览网站.在此过程中,我需要按一些按钮. 首先,我使用以下xml构建按钮的HtmlAnchor对象:
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>
当我使用click()方法单击它时,它可以正常工作. 然后,我转到另一个页面,在该页面上我具有需要单击的链接才能显示所需的内容. 单击后,我没有移到另一个页面,它只是一个Java脚本函数触发.
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>
对于这两个元素,我都使用HtmlAnchor对象及其click()方法.但是该方法对第二个元素什么也没做.
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.
我也尝试使用HtmlUnit内置的JavaScript引擎,但没有成功. 如何在HtmlUnit平台上单击此永久链接?
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?
推荐答案
最可能的问题是HtmlUnit没有等待JavaScript完成运行. HtmlUnit常见问题解答建议了三种解决方法: http://htmlunit.sourceforge.net/faq.html#AJAXDoesNotWork .
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.
其中,最尝试的解决方案是让您的WebClient
等待AJAX请求完成:
Of these, the neatest solution to try is to get your WebClient
to wait for AJAX requests to finish:
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
我发现有些站点在后台线程上运行AJAX的技巧很巧妙-这意味着NicelyResynchronizingAjaxController
不会等待它完成,因为它只监视主UI线程. 在这里有一个很好的答案,它解释了如何等待所有线程而不是仅等待主线程.
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.
这篇关于Htmlunit中的HtmlAnchor click()函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!