LordBullington
LordBullington

Reputation: 219

"Click" on website button

I want to parse a list in a website with Java. I already know how to parse the list with Jsoup - so this part is done and working.

However, to show the list in the web page I need to press a button and I don't know how to do that with Java. I already found a similar question on SO: Programmatically click a webpage button. I think I could use HttpURLConnection and KVP for this but I don't know how to get it to work.

The list selector looks like this: I would like to press Show all button I would like to press the Show all button.

Code which I found in the html file:

<div class="row top-buffer">
    <div class="col-md-6 col-sm-4 col-xs-6 nopadding">
        <button type="button" id="clearselection" class="btn btn-knx" style="float: right; margin: 0px">Clear selection</button>
    </div>
    <div class="col-md-6 col-sm-4 col-xs-6 nopadding">
        <button type="button" id="showall" class="btn btn-knx align-right" style="float: left">Show all</button>
    </div>
</div>

Does anybody know how this is done? ..it is btw not only one button which I need to press in this website otherwise I would do it manually. Once I know how it is done with one button I will be able to figure it out for the other ones too.

UPDATE: I want to point out that I don't have access to the HTML file. I basically want to simulate a button click of a website visitor. I'm sorry I was not clear.

UPDATE-2: I have just now started working on this again. I found the Javascript code for the button in the web page:

$('#showall').on('click', function () {
            $('.mask').show();
            $('#name').attr('value', '');
            $("#letter option").removeAttr('selected');
            $("#letter option[value='']").attr('selected', true);
            $.ajax({
                url: '/src/international/Data/partListAjax',
                type: 'POST',
                dataType: "html",
                data: {
                    data: $('#filterForm').serialize(),
                    wTexts: {"Company":"Company","Country":"Country","Name":"Name","Total":"Total","search":"Search","matches_found":"matches found","search_no_results":"Your search did not match any results."},
                    national: 'null',
                    currentProject: 'nma-en'
                },
                success: function (data) {
                    if (data.indexOf("Allowed memor") >= 0) {
                        $('#accordion2').html("<div class='alert alert-warning' role='alert'>Please use our search engine</div>");
                        $('.mask').hide();
                        $('#clearselection').attr('disabled', 'disabled');
                        $('#showall').attr('disabled', 'disabled');
                    } else {
                        $('#accordion2').html(data);
                        $('.mask').hide();
                        $('#clearselection').attr('disabled', 'disabled');
                        $('#showall').attr('disabled', 'disabled');
                    }
                }
            });
        });

If invoking the button function doesn't work, I will try to directly send/trigger the Ajax command which is included in the function.

Upvotes: 0

Views: 1625

Answers (2)

LordBullington
LordBullington

Reputation: 219

I finally got it to work by using Selenium WebDriver. I just saw one tutorial on YouTube and within some hours everything was done. Incredible. :)

Upvotes: 0

user9081948
user9081948

Reputation:

LordBullington, i have done this on my website as well, below solution is working perfectly for me.

First you need send a variable to the UI.

if you are using servlet, you can do that by

request.setAttribute("clickbutton", "yes");

if you are using Spring, you can do that by

Map<String, Object> model = new HashMap<String, Object>();
model.put("clickbutton", "yes");
return new ModelAndView("yourView", model);

Then in your html, keep a hidden variable to receive the variable sent from JAVA.

<input type="hidden" id="clickbutton" value="${clickbutton}">

Then use Jquery to trigger the button on page load. Make sure this jquery code is after the hidden input tag.

$( document ).ready(function() {
    if(('#clickbutton').val()=='yes') {

       //use your button id here
       $("#youButtonIdHere").click();
    }
});

Upvotes: 1

Related Questions