如何单击加载到JavaFX Webengine的网站上的按钮

时间:2014-07-20 06:18:19

标签: java javascript javafx javafx-2 javafx-8

我是JavaFX的新手。我已成功将网站加载到我的JavaFX应用程序中。我想从我的java应用程序登录到该网站。 到目前为止,我能够将我的登录值插入相应的输入框,但我无法弄清楚如何单击提交按钮。有人能帮我吗。 这是我到目前为止所拥有的。 enter image description here

这是我的控制器类。

    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??

            }
        });


    }
}

有人可以帮助我。我该怎么办才能提交? 感谢。

1 个答案:

答案 0 :(得分:3)

我在JavaFX的WebView组件中使用html表单的示例找到了此链接:https://community.oracle.com/thread/2510161?start=0&tstart=0

以下示例将获取对表单的引用并提交。

我无法让gmail完全加载到WebView组件内部,因为来自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();
    }
}