自动填充Java中的WebView字段

时间:2013-09-26 20:45:47

标签: java swing webview javafx field

所以这是我的问题。我正在使用JavaFX中的WebView类。我想要做的是,我希望webview中加载的字段自动填充存储在数组中的信息。可能吗? 提前致谢

2 个答案:

答案 0 :(得分:6)

这是一个用于WebView的自动表单填写示例JavaFX应用程序。

值(登录凭证)输入到屏幕黄色部分的JavaFX字段中,然后在登录页面出现时自动发布(使用w3c dom api)WebView(屏幕的白色部分)。

loginpost

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javafx.stage.Stage;
import org.w3c.dom.*;
import org.w3c.dom.html.*;

public class WebViewFormPost extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        final TextField fxUsername = new TextField();
        fxUsername.setPrefColumnCount(20);
        final TextField fxPassword = new PasswordField();

        final BooleanProperty loginAttempted = new SimpleBooleanProperty(false);

        final WebView webView = new WebView();
        webView.setPrefWidth(1000);
        final WebEngine engine = webView.getEngine();
        engine.documentProperty().addListener(new ChangeListener<Document>() {
            @Override
            public void changed(ObservableValue<? extends Document> ov, Document oldDoc, Document doc) {
                if (doc != null && !loginAttempted.get()) {
                    if (doc.getElementsByTagName("form").getLength() > 0) {
                        HTMLFormElement form = (HTMLFormElement) doc.getElementsByTagName("form").item(0);
                        if ("/oam/server/sso/auth_cred_submit".equals(form.getAttribute("action"))) {
                            HTMLInputElement username = null;
                            HTMLInputElement password = null;
                            NodeList nodes = form.getElementsByTagName("input");
                            for (int i = 0; i < nodes.getLength(); i++) {
                                HTMLInputElement input = (HTMLInputElement) nodes.item(i);
                                switch (input.getName()) {
                                    case "ssousername":
                                        username = input;
                                        break;
                                    case "password":
                                        password = input;
                                        break;
                                }
                            }

                            if (username != null && password != null) {
                                loginAttempted.set(true);
                                username.setValue(fxUsername.getText());
                                password.setValue(fxPassword.getText());
                                form.submit();
                            }
                        }
                    }
                }
            }
        });
        engine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
            @Override
            public void changed(ObservableValue<? extends Throwable> ov, Throwable oldException, Throwable exception) {
                System.out.println("Load Exception: " + exception);
            }
        });

        GridPane inputGrid = new GridPane();
        inputGrid.setHgap(10);
        inputGrid.setVgap(10);
        inputGrid.addRow(0, new Label("Username: "), fxUsername);
        inputGrid.addRow(0, new Label("Password: "), fxPassword);

        Button fxLoginButton = new Button("Login to Oracle Forums");
        fxLoginButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                if (notEmpty(fxPassword.getText()) && notEmpty(fxPassword.getText())) {
                    loginAttempted.set(false);
                    engine.load("https://forums.oracle.com/community/developer/english/java/javafx/login.jspa");
                }
            }
        });
        fxLoginButton.setDefaultButton(true);
        ProgressIndicator fxLoadProgress = new ProgressIndicator(0);
        fxLoadProgress.progressProperty().bind(webView.getEngine().getLoadWorker().progressProperty());
        fxLoadProgress.visibleProperty().bind(webView.getEngine().getLoadWorker().runningProperty());

        HBox loginPane = new HBox(10);
        loginPane.getChildren().setAll(
                fxLoginButton,
                fxLoadProgress
        );

        final VBox layout = new VBox(10);
        layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
        layout.getChildren().addAll(
                new Label("Enter your Oracle Web Account credentials"),
                inputGrid,
                loginPane,
                webView
        );
        VBox.setVgrow(webView, Priority.ALWAYS);

        stage.setScene(new Scene(layout));
        stage.show();

        fxUsername.requestFocus();
    }

    private boolean notEmpty(String s) {
        return s != null && !"".equals(s);
    }
}

上述应用程序改编自Submitting HTML Forms with JavaFX Webview上之前的Oracle论坛问题。

如果您没有Oracle技术网络帐户来测试上述程序,可以在此处注册一个:https://myprofile.oracle.com/EndUser/faces/profile/createUser.jspx

使用JQuery发布到WebView

另一种实现,我实际上更喜欢使用JavaScript jQuery来内省DOM并执行帖子而不是使用Java DOM apis。有一个使用jQuery on any arbitrary webpage hosted in a WebView的示例。因此,您可以将来自此自动WebView表单帖子和jQuery托管WebView示例的想法组合在一起,以创建使用JQuery执行帖子的版本。

答案 1 :(得分:5)

我用JavaFX webView Javascript引擎修复了这个问题。 如果有人对这里的代码片段感兴趣。

    String setLastName  =  "document.getElementsByName('lastName')[0].value='" + lastName + "';";
    String setName =  "document.getElementsByName('firstName')[0].value='" + name + "'";
    String setDateBirth = "document.getElementsByName('birthdate')[0].value='" + datebirth + "';";
    String setPhone = "document.getElementsByName('phone')[0].value='" + phone + "';";
    String setEmail = "document.getElementsByName('email')[0].value='" + email + "';";
    String setPassport = "document.getElementsByName('passport')[0].value='" + passport + "';";
    Button button = new Button("Fill the form");

    button.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            webEngine.executeScript(setLastName);
            webEngine.executeScript(setName);
            webEngine.executeScript(setDateBirth);
            webEngine.executeScript(setPhone);
            webEngine.executeScript(setEmail);
            webEngine.executeScript(setPassport);
        }
    });