5

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" /> 
1
  • The second parameter of UrlEncodedFormEntity should be java.nio.charset.StandardCharsets.UTF_8 since HTTP.UTF_8 has been deprecated. Commented Jul 6, 2013 at 19:36

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.