加载到JavaFXWebEngine中的网站

加载到JavaFXWebEngine中的网站,javafx,login,webview,Javafx,Login,Webview,我想让我的申请自动登录kolotibablo网站,但该网站不存在标签表单。我应该如何提交?谢谢 @Override public void initialize(URL url, ResourceBundle rb) { // WebEngine engine = browser.getEngine(); engine.load("https://www.kolotibablo.com/panel/login#login");

我想让我的申请自动登录kolotibablo网站,但该网站不存在标签表单。我应该如何提交?谢谢

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //
        WebEngine engine = browser.getEngine();
        engine.load("https://www.kolotibablo.com/panel/login#login");
        engine.setJavaScriptEnabled(true);

        engine.documentProperty().addListener(new ChangeListener<Document>() {
            @Override
            public void changed(ObservableValue<? extends Document> observable, Document oldValue, Document newValue) {
                engine.getDocument().getElementById("login").setAttribute("value", "User Name");
                engine.getDocument().getElementById("password").setAttribute("value", "Password");
            //Login
            }
        });

    }
@覆盖
公共void初始化(URL、ResourceBundle rb){
//
WebEngine=browser.getEngine();
发动机负荷(“https://www.kolotibablo.com/panel/login#login");
engine.setJavaScriptEnabled(true);
engine.documentProperty().addListener(新的ChangeListener()){
@凌驾

public void changed(observevalue我通过使用下面的代码成功地做到了这一点。我没有使用您的
引擎.documentPorperty()
侦听器,所以我完全删除了该代码

在我的帮助下,我可以做一些编辑

engine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
    if ( newState == Worker.State.SUCCEEDED ) {
        Element login = engine.getDocument().getElementById("login");
        if ( login != null ) {
            login.setAttribute( "Value", "User_Name" );
            System.out.println(login.getAttribute("Value"));
        }
        Element password = engine.getDocument().getElementById( "password" );
        if ( password != null ) {
            password.setAttribute( "Value", "Password" );
            System.out.println(password.getAttribute("Value"));
        }
        HTMLElementImpl loginButton = (HTMLElementImpl) engine.getDocument().getElementById( "login_button" );

        if ( loginButton != null ) {
            loginButton.addEventListener("click", event -> {
                System.out.println("here " + event.getType());
            }, false);
            loginButton.click();
        }
    }
});
因此,这将等待工作状态成功,然后抓取
login
元素,设置属性,抓取
password
元素,设置属性,抓取
loginButton
作为
HTMLElementImpl
,这允许我们模拟单击它

我在打印报表中留下了内容,以便您可以看到事情发生时正在进行

希望这有帮助

编辑1

因此,我重新审视了这个问题,并提出了这个解决方案

String username = "User_Name";
String password = "Password";
String enterUserName = "document.getElementById('login').value='" + username + "';";
String enterPw = "document.getElementById('password').value='" + password + "';";;
engine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
    if ( newState == Worker.State.SUCCEEDED ) {
        engine.executeScript(enterUserName);
        engine.executeScript(enterPw);
    }
});
我唯一搞不懂的是如何单击登录按钮。我尝试的一切都失败了。因此,我对此感到抱歉。但是,这里还有另一种方法。请使用

HTMLFormElement loginForm=(HTMLFormElement)webEngine.getDocument().getElementById(“登录”); loginForm.submit();
要登录,您必须提交表单,在上面的代码中“login”是表单id,下载完成后,只有网站密码才能设置属性login。setAttribute(“Value”,“User_Name”);它不安装
登录
必须使用不同的
属性
。我将无法查看,直到tomorrow@KiênĐịnh已编辑。由于某些原因,登录按钮不会单击或提交,但用户名现在将按预期输入。