Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open IE from java and perform operations like click() etc. through java?

I wan't to login to a website through Java and perform operations, like click, add text to a textfield, etc., through Java.

like image 467
Yatendra Avatar asked Apr 30 '26 05:04

Yatendra


2 Answers

I suggest using a testing framework like HtmlUnit. Even through it's designed for testing, it's a perfectly good programmatic "navigator" of remote websites.

Here's some sample code from the site, showing how to navigate to a page and fill in a form:

public void submittingForm() throws Exception {
   WebClient webClient = new WebClient();
   HtmlPage page1 = webClient.getPage("http://some_url");
   HtmlForm form = page1.getFormByName("myform");
   HtmlSubmitInput button = form.getInputByName("submitbutton");
   HtmlTextInput textField = form.getInputByName("userid");
   textField.setValueAttribute("root");
   HtmlPage page2 = button.click();
}
like image 117
skaffman Avatar answered May 02 '26 18:05

skaffman


You could launch it by

Runtime.getRuntime().exec("command-line command to launch IE");

then use Java's Robot class to send mouse clicks and fill in text. This seems rather crude, though, and you can probably do better by communicating directly with the web server (bypassing the browser entirely).

like image 30
Bill the Lizard Avatar answered May 02 '26 18:05

Bill the Lizard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!