Submit HTML Form in Java

10,449

Solution 1

You don't "click a button" with HttpClient; all it does is HTTP stuff, which is unrelated to JS and the DOM.

If you want to emulate a browser, use something like JWebUnit, which can drive both HttpClient and Selenium, and provides JavaScript support.

Solution 2

Your submit button calls a javascript function when clicked. You can't emulate that behaviour using your java code.

For that you need to use a headless browser like htmlunit which can handle javascript.

Share:
10,449
Koepasso
Author by

Koepasso

Updated on June 04, 2022

Comments

  • Koepasso
    Koepasso almost 2 years

    I am trying to fill in a HTML Form, press a submit button of the form and then get the response from it.

    Filling in the form works really well but i can't figure out how to press the submit button on the page.

    I am using the apache httpclient libraries.

    My code is:

            httpclient = new DefaultHttpClient();
            HttpPost httpost = new HttpPost(pUrl);
    
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("filter_response_time_http", "1"));
            nvps.add(new BasicNameValuePair("filter_port", "80"));
            nvps.add(new BasicNameValuePair("filter_country", "US"));
            nvps.add(new BasicNameValuePair("submit", "Anzeigen"));
    
            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    
            response = httpclient.execute(httpost);
    
    
            entity = response.getEntity();
    

    The code for the submit button is:

    <input onclick="doSubmit();" id="submit" type="submit" value="Anzeigen" name="submit" />