0

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.

LordBullington
  • 219
  • 1
  • 2
  • 11
  • 1
    You will need a browser client of some sort, how about a webview. see this, https://stackoverflow.com/questions/30113445/how-to-perform-a-button-click-inside-a-webview-android – petey Dec 28 '17 at 14:56
  • 1
    Please go through this, https://www.codeproject.com/Articles/392603/Android-addJavaScriptInterface – Pramod Waghmare Dec 28 '17 at 14:58
  • 1
    Removed the abusive `android` tag – Phantômaxx Dec 28 '17 at 16:37
  • Thanks for the links. But in these scenarios the developers have access to the HTML file which I don't have. I just have the id for the button which I found in the source code of the webpage. – LordBullington Dec 29 '17 at 07:25

2 Answers2

1

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();
    }
});
  • Thanks for your answer but I don't have access to the HTML file. Can I still simulate a button click with the button info (id) I have? Or maybe there is a tool to analyze the communication with the website. So I could find out what is send when pressing the button. – LordBullington Dec 29 '17 at 07:30
0

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

LordBullington
  • 219
  • 1
  • 2
  • 11