如何使用 htmlunit 单击 javascript 按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3897871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I click a javascript button with htmlunit?
提问by Laxsnor
I'm working on an application that will automatically click a button on a webpage using htmlunit in Java. Only problem is that that button is a javascript button, so the standard getInputByName() won't work. Any suggestions with dealing with this? The code for the button is included below.
我正在开发一个应用程序,该应用程序将使用 Java 中的 htmlunit 自动单击网页上的按钮。唯一的问题是该按钮是一个 javascript 按钮,因此标准 getInputByName() 将不起作用。有什么处理这个问题的建议吗?按钮的代码包含在下面。
<a class="vote_1" id="1537385" href="/javascript%3Avoid%280%29/index"><img src="/images/parts/btn-vote.gif" alt="Btn-vote" /></a>
In addition, here's the other code for voting.
此外,这是投票的其他代码。
<div id="content"><script type="text/javascript" src="/js/scriptFeeds/voteArticle.js"></script>
Which leads to the following javascript:
这导致以下javascript:
var pressed = new Array();
$j(document).ready(function() {
var nr = $j("input#number_of_articles").val();
for(var i=1; i<=nr; i++){
$j("a.vote_"+i).click(function(){
var article = $j(this).attr("id");
$j('#'+article).hide();
if (!pressed[article]) {
pressed[article] = "yes";
jQuery.post('/vote-article', {
_token: $j("#_token").val(),
article_id: article
},function(data) {
$j("span.numberOfVotes_"+data.id).html(data.votes);
}, "json");
}
return false;
});
}
});
回答by Clyde Lobo
Try using this addOn for firefox, it records your actions and generates the HTMLUnit code for the same. may be it could help. http://code.google.com/p/htmlunitscripter/
尝试将此插件用于 Firefox,它会记录您的操作并生成相同的 HTMLUnit 代码。可能会有所帮助。 http://code.google.com/p/htmlunitscripter/
回答by Rodney Gitzel
There's nothing special about clickable images. Something like this should work:
可点击的图像没有什么特别之处。这样的事情应该工作:
button = page.getHtmlElementById( "1537385" ) ;
page = button.click() ;
HtmlUnit will then run the Javascript and return the updated page.
HtmlUnit 然后将运行 Javascript 并返回更新的页面。
If the id attribute of the 'a' tag isn't constant, you may need to use XPath to grab it.
如果 'a' 标签的 id 属性不是常量,您可能需要使用 XPath 来获取它。
回答by Matt G
I have a very similar link on one of my pages. If you can call .click() on any HtmlElement, it should be able to run associated Javascript. Here is my code (generated from HtmlUnitScripter):
我的其中一个页面上有一个非常相似的链接。如果您可以在任何 HtmlElement 上调用 .click(),它应该能够运行关联的 Javascript。这是我的代码(从 HtmlUnitScripter 生成):
HtmlElement element4 = null;
Iterable<HtmlElement> iterable5 = page.getAllHtmlChildElements();
Iterator<HtmlElement> i6 = iterable5.iterator();
while(i6.hasNext())
{
HtmlElement anElement = i6.next();
if(anElement instanceof HtmlImage)
{
HtmlImage input = (HtmlImage) anElement;
String[] elements = "http://example.com/pages/powerbutton.png".split( "/" );
if(input.getSrcAttribute().indexOf(elements[elements.length-1] )> -1 )
{
element4 = input;
break;
}
}
}
HtmlPage page = element4.click();

