0

Is it possible in Java to launch a browser and navigate to a specific URL then execute some Javascript on that page?

Currently I've got this:

public class Launcher {


    public static void main(String[] args){
        try{
          Desktop.getDesktop().browse(new URI("www.facebook.com"));
        }
        catch(IOException e) {
            System.out.println(e.getMessage());
        } catch (URISyntaxException e) {
            System.out.println(e.getMessage());
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }


}

This, for example brings me to the facebook website.

In Javascript I could do now like this:

document.getElementById("email").setRangeText("loginName");

To fill in my username.

My question is, is this possible to do from inside Java once you opened the webpage through the desktop.browse() ?

EDIT2: can I navigate using this Desktop.browse() ? What if I want to go to a different url?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
  • Try apache httpclient. let us know if that suits. – Hirak May 28 '14 at 13:09
  • see [`Desktop.browse()`](http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#browse(java.net.URI)) – vandale May 28 '14 at 13:17
  • http://stackoverflow.com/questions/5226212/how-to-open-the-default-webbrowser-using-java. This link should help you – Dinal May 28 '14 at 13:36
  • Okay so Desktop.browse() let's me open a browser to a specific url. Can I execute some javascript after I browsed to a website? – user3673517 May 28 '14 at 13:37
  • You can try to move and click mouse using the awt Robot class, I've never tried it but it could work. Maybe. Either way, it's a suspicious thing you're trying to do, and a weird approach. – MightyPork May 28 '14 at 20:08
  • btw, why dont you just write a browser plugin in javascript? it will make much more sense. – MightyPork May 28 '14 at 20:11
  • Can't use pure javascript since , I also need to read files from disk. Eventualy I went with webspec watij and that does the trick. Thanks everybody – user3673517 May 29 '14 at 08:09

0 Answers0