How to click a button on website loaded into JavaFX Webengine


I am pretty new to JavaFX. I have successfully loaded a website into my JavaFX application. I would like to log in to the website from my java application. So far I am able to insert my login values to the appropriate input box, but I cannot figure out how to click submit button. Can Someone help me. Here is what I have so far. enter image description here

Here is my controller class.

    public class Controller implements Initializable{

    protected TextField usernameLogin;
    protected TextField passwordLogin;
    protected Button loginButton;
    protected WebView webviewBrowser;
    public void initialize(URL location, ResourceBundle resources) {

        final WebEngine engine = webviewBrowser.getEngine();

        loginButton.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {

                String username = usernameLogin.getText().toString();
                String password = passwordLogin.getText().toString();

                engine.getDocument().getElementById("Email").setAttribute("Value", username);
                engine.getDocument().getElementById("Passwd").setAttribute("Value", password);

                //engine.getDocument().getElementById("submit"). ??submit code??



Can someone assist me. What should I do to submit? Thanks.

7/20/2014 6:18:18 AM

Accepted Answer

I found this link with an example of working with html forms in JavaFX's WebView component:

The below example will obtain a reference to the form and submit it.

I was not able to actually get gmail to fully load inside of the WebView component because the of an error from jfxwebkit.dll.

import java.util.concurrent.atomic.AtomicBoolean;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.w3c.dom.Element;
import org.w3c.dom.html.HTMLFormElement;

public class Main extends Application {
    public void start( Stage stage ) throws Exception {
        stage.setTitle( "JavaFX WebView Example" );
        BorderPane borderPane = new BorderPane();
        WebView webviewBrowser = new WebView();
        borderPane.setCenter( webviewBrowser );
        final WebEngine engine = webviewBrowser.getEngine();
        engine.load( "" );
        engine.setJavaScriptEnabled( true );
        final String username = "";
        final String password = "password";
        final AtomicBoolean submitted = new AtomicBoolean();
                new ChangeListener<Worker.State>() {
                    public void changed( ObservableValue<? extends Worker.State> ov,
                                         Worker.State oldState, Worker.State newState ) {
                        if ( newState == Worker.State.SUCCEEDED ) {
                            Element emailField = engine.getDocument().getElementById( "Email" );
                            if ( emailField != null ) {
                                emailField.setAttribute( "Value", username );
                            Element passwordField = engine.getDocument().getElementById( "Passwd" );
                            if ( emailField != null ) {
                                passwordField.setAttribute( "Value", password );
                            HTMLFormElement gaia_loginform = ( HTMLFormElement ) engine.getDocument().getElementById( "gaia_loginform" );
                            if ( gaia_loginform != null ) {
                                if ( !submitted.get() ) {
                                    submitted.set( true );
        stage.setScene( new Scene( borderPane, 800, 600 ) );;
7/31/2014 5:26:01 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow