Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

java - How do I click a javascript button with htmlunit?

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.

<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:

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;
    });
}
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...