3

I am working an an JavaFX Webbrowser that can autologin to some sites, i know how to set the data to username and password fields but how to i make it execute the login button click? This is what i got so far:

String email  =  "document.getElementsByName('email')[0].value='MY_EMAIL';";
            String pass =    "document.getElementsByName('pass')[0].value='MY_PASSWORD';";
            String login =   "";
            webEngine.executeScript(email);
            webEngine.executeScript(pass);
            webEngine.executeScript(login);

and this is the javascript code of the button it should click:

<label class="uiButton uiButtonConfirm" id="loginbutton" for="u_0_c"><input value="Aanmelden" tabindex="4" type="submit" id="u_0_c"></label>  
Piet Jetse
  • 388
  • 1
  • 5
  • 16

2 Answers2

8

this is a concentrated, non-specialized example... uses the dom.w3c.Node.* package

HTMLInputElement element = (HTMLInputElement)myWebView.getEngine().getDocument().getElementsByTagName("input").item(0);
element.click();

Find a way to handle the object you're looking for, and it will work.

EverNight
  • 964
  • 7
  • 16
  • I'm getting a `com.sun.webkit.dom.HTMLButtonElementImpl cannot be cast to org.w3c.dom.html.HTMLInputElement` any idea? – sirolf2009 Nov 01 '18 at 18:22
  • as far as i remember, button elements and input elements are not extensions of one another. I think you can solve this by checking the class first, then casting your element accordingly. Use instanceof or getClass().equals(someElement.class) or similar. Should work. – EverNight Nov 02 '18 at 11:42
2

I haven't tried that, but it should work. The idea is add jQuery to the loaded page and then to use it to click the button. This post explains how to do this with JS: How Do I Add jQuery To Head With JavaScript? So with Java it should be (I've copied the first answer into a string and executing it with the web engine):

String script = "script = document.createElement('script');\n" +
    "\n" +
    "script.onload = function() {\n" +
    "    // jQuery is available now\n" +
    "};\n" +
    "var head = document.getElementsByTagName(\"head\")[0];\n" +
    "\n" +
    "script.type = 'text/javascript';\n" +
    "script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';\n" +
    "\n" +
    "head.appendChild(script);";

webEngine.executeScript(script);

This should be done right when WebView is initialized (via webEngine.getLoadWorker().stateProperty().addListener(...)). Note: jQuery is loaded asynchronously.

Now you should be able to click a button with jQuery:

webEngine.executeScript("$('#buttonId')[0].click()");

This post should help you with debugging JS in a WebView: JAVAFX / WebView / WebEngine FireBugLite or Some other debugger?

Community
  • 1
  • 1
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
  • Im getting an error: netscape.javascript.JSException: ReferenceError: Can't find variable: head at com.sun.webpane.platform.WebPage.twkExecuteScript(Native Method) at com.sun.webpane.platform.WebPage.executeScript(WebPage.java:1438) at javafx.scene.web.WebEngine.executeScript(WebEngine.java:811) – Piet Jetse Dec 08 '13 at 20:04
  • The error means that `head` is undeclared. I've updated code samples and checked that it is working. In FirebugLite you could check your jQuery selectors, i.e. type in a console `jQuery('body')` or `$('body')`. – Andrey Chaschev Dec 08 '13 at 20:42
  • The fixed the error thanks but i got another error now: netscape.javascript.JSException: TypeError: 'undefined' is not an object at com.sun.webpane.platform.WebPage.twkExecuteScript(Native Method) at com.sun.webpane.platform.WebPage.executeScript(WebPage.java:1438) at javafx.scene.web.WebEngine.executeScript(WebEngine.java:811) – Piet Jetse Dec 09 '13 at 13:28
  • This is a JS error, like NullPointerException in Java. May be `buttonId` or the button has a different id? You can test jQuery selectors in FirebugConsole: type `$('#buttonId')` to checks if button exists. – Andrey Chaschev Dec 09 '13 at 13:36