How to click a button on website loaded into JavaFX Webengine


Question

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{


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

        final WebEngine engine = webviewBrowser.getEngine();
        engine.load("http://gmail.com");
        engine.setJavaScriptEnabled(true);

        loginButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            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.

1
4
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: https://community.oracle.com/thread/2510161?start=0&tstart=0.

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 {
    @Override
    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( "http://gmail.com" );
        engine.setJavaScriptEnabled( true );
        final String username = "324jlk23j4l2klj34l2kj34l2jk34l2kj3lkjlajksdflkj@gmail.com";
        final String password = "password";
        final AtomicBoolean submitted = new AtomicBoolean();
        engine.getLoadWorker().stateProperty().addListener(
                new ChangeListener<Worker.State>() {
                    @Override
                    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 );
                                    gaia_loginform.submit();
                                }
                            }
                        }
                    }
                }
        );
        stage.setScene( new Scene( borderPane, 800, 600 ) );
        stage.show();
    }
}
3
7/31/2014 5:26:01 PM

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