5

How to programmatically click a webpage button in Java?

Given this button:

 <div id="titlebutton" >Button</div>

In Javascript it would work like this:

var newWin = window.open(siteYouHaveAccessTo);
newWin.document.getElementById('titlebutton').click();

I was wondering how I would do this in Java, not Javascript (Is there a way of doing it with Java alone or do I have to import/use Javascript?).

Edit:

Clicking the Button will result in a call of a function called setText(). I am not sure how I would send a similar request as this function? Would it help if I pasted the function code?

1478963
  • 1,198
  • 4
  • 11
  • 25
  • 8
    You wouldn't click a button, you would send an HTTP request similar to the one that would be sent if the button was clicked. – Sotirios Delimanolis May 16 '13 at 13:58
  • 2
    Java in what environment? As an applet in (another) web page? As a standalone UI application? In a server-side headless Java program? – T.J. Crowder May 16 '13 at 13:58
  • Standalone application. – 1478963 May 16 '13 at 14:01
  • 1
    @Jeshurun He means an HTML button. Whether it's a swing UI makes no difference for sending HTTP requests. With the keywords in my first comment, you should be able to google for how to make an HTTP request with java. – Sotirios Delimanolis May 16 '13 at 14:04
  • 1
    I don't get it. What does Java have to do with clicking a HTML button? Your browser is not going to execute it unless it is an applet. – Jeshurun May 16 '13 at 14:05
  • Yes, please paste the function code, or at least a snippet. – Jeshurun May 16 '13 at 14:07
  • You can excute a webPage request in a standalone application using the following: HttpClient httpClient = new DefaultHttpClient(); HttpGet httpRequest = new HttpGet(url); HttpResponse response = httpClient.execute(httpRequest); – 1478963 May 16 '13 at 14:07
  • @Jeshurun What happens behind the scenes when you click a button on a webpage? The browser takes the form parameters and builds an HTTP request that it then sends to a URL. You need to do the same thing through java. Find out which parameters need to be sent along with the request and add those as request parameters, with your `HttpGet`. – Sotirios Delimanolis May 16 '13 at 14:08
  • How would I immitate the HTTP request? The button does not really tell me anything, and I am sure the setText() function is called but how do I imitate that request. – 1478963 May 16 '13 at 14:09
  • @user2100799 You posted the starting code in your previous comment. Look at your web page and check if you need to enter more input before clicking the button. If you do, those inputs should also be request parameters in your HTTP request. Or, maybe, look at the javascript that gets executed with the button click (use firebug to check the request). – Sotirios Delimanolis May 16 '13 at 14:11
  • To start: http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily – Sotirios Delimanolis May 16 '13 at 14:13
  • @SotiriosDelimanolis No text is inputted at all. Once the site is loaded, to generate random text you click the button. That is all. – 1478963 May 16 '13 at 14:14
  • @user2100799 The button click executes in browser javascript or sends an ajax request to generate the text? – Sotirios Delimanolis May 16 '13 at 14:16
  • @SotiriosDelimanolis I believe its javascript. – 1478963 May 16 '13 at 14:20
  • 1
    @user2100799 As far as I know, you cannot do this. On it's own, the button is just HTML text. It has value in a browser, which converts it into a clickable UI element. Since java is not going to interact with the browser, you basically don't have access to it. Javascript runs in the browser, so no access to that either. – Sotirios Delimanolis May 16 '13 at 14:22
  • @SotiriosDelimanolis Ah... I see, in that case I am sorry in wasting your time :'(. Thanks a lot for your help! – 1478963 May 16 '13 at 14:26
  • cannot use apache http request if you want the other browser conditions to be met - a client side UI and java script execution. Can use it to send HTTP requests to a server. Maybe you should first describe you top level situation and requirement first – tgkprog May 16 '13 at 16:27

3 Answers3

7

You can't 'programmatically' click a button with Java alone, which is why we use JavaScript. If you want to get into controlling the browser such as clicking buttons and filling text fields you would have to use an automation tool. An automation tool that uses Java is Selenium. This is typically used as for test automation, but will do what you want it to do.

See http://docs.seleniumhq.org/

0

I'm not sure if this is what you are looking for, but if what you are looking to do is simply make a HTTP request in Java, this should do the trick:

HttpURLConnection urlConnection = null;
URL urlToRequest = new URL("http://yoursite.com/yourpage?key=val&key1=val1");
urlConnection = (HttpURLConnection) urlToRequest.openConnection();
urlConnection.setConnectTimeout(CONN_TIMEOUT);
urlConnection.setReadTimeout(READ_TIMEOUT);

// handle issues
int statusCode = urlConnection.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
    // handle unauthorized (if service requires user login)
} else if (statusCode != HttpURLConnection.HTTP_OK) {
    // handle any other errors, like 404, 500,..
}

InputStream in = new BufferedInputStream(urlConnection.getInputStream());

// Process response

Replace the timeout constants with your own values, and the URL to match that of your website. You can send any data you want to send with the click of the button as query parameters in the URL in the form of key/value pairs.

Jeshurun
  • 22,940
  • 6
  • 79
  • 92
0

if you looking for browser automation can use selenium or java.awt.Robot

Robot can send 'clicks' to the OS. have used it along with vbs scripts to first make sure a particular window has focus and then send text and clicks, and finally save results ... not very good as if the page changes things then you need to make adjustments. But the person I made this used it for 4 years.

tgkprog
  • 4,493
  • 4
  • 41
  • 70