java - 在 Java 中自动填充字段 WebView

标签 java swing webview javafx field

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

最佳答案

这是一个用于 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内省(introspection) DOM 并执行发布而不是使用 Java DOM api。有一个使用 jQuery on any arbitrary webpage hosted in a WebView 的示例.因此,您可以结合来自这个自动 WebView 表单发布的想法和 jQuery 托管的 WebView 示例来创建一个使用 JQuery 执行发布的版本。

关于java - 在 Java 中自动填充字段 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19038369/

相关文章:

Java Swing : implementing my Comboboxmodel for JComboBox

java - 将 JFrame 保存为图像文件(.JPG、.PNG、.BMP 等)

java - 在java中插入换行符

ios - 在应用程序 IOS Objective C 启动时添加新的 View Controller

java - 将参数传递给Java中的main方法

java - 以给定的延迟连续运行函数

java - QueryDSL 获取另一个实体集合中的任何实体

java - 学习 Ruby on Rails(已获得面向对象的知识)

android:webView 处理方向变化

javascript - 提高 webview 性能 - android