使用WebEngine.executeScript函数单击JavaFX中的链接不起作用

使用WebEngine.executeScript函数单击JavaFX中的链接不起作用,java,javafx-2,Java,Javafx 2,我需要帮助了解如何使用javaFx WebEngine.executeScript正确单击链接。 我尝试了以下代码:webEngine.executeScript(“document.querySelectorAll(\'a[ajaxify^='/ajax/messaging/composer.php?']\”)[0]。单击();” 但是,每次运行应用程序时,在执行上述代码时都会生成以下错误。错误如下: netscape.javascript.JSException: TypeError: 'u

我需要帮助了解如何使用javaFx WebEngine.executeScript正确单击链接。 我尝试了以下代码:
webEngine.executeScript(“document.querySelectorAll(\'a[ajaxify^='/ajax/messaging/composer.php?']\”)[0]。单击();”

但是,每次运行应用程序时,在执行上述代码时都会生成以下错误。错误如下:

netscape.javascript.JSException: TypeError: 'undefined' is not a function
    at com.sun.webpane.platform.WebPage.twkExecuteScript(Native Method)
    at com.sun.webpane.platform.WebPage.executeScript(WebPage.java:1438)
    at javafx.scene.web.WebEngine.executeScript(WebEngine.java:811)
    at javafxapplication2.MainController$4.run(MainController.java:201)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    at java.lang.Thread.run(Thread.java:722)

我还验证了该对象是否存在,但由于某种原因,对其调用click()函数不起作用。提前感谢您的帮助。

我正在使用JavaFX8,但应该是相同的想法。如果创建MouseEvents对象,则应该能够单击任何元素

this.webEngine.executeScript("(function () {" +
            "var element = document.querySelector('a');" +
            "var bounds = element.getBoundingClientRect();" +
            "var evt = document.createEvent('MouseEvents');" +
            "evt.initMouseEvent('click', true, false, window," +
            "1, 0, 0, bounds.left, bounds.top," +
            "false, false, false, false," +
            "0, null);" +
            "element.dispatchEvent(evt);" +
            "}());");

我猜您在加载文档之前正在运行调用

要知道何时加载文档,可以在或WebEngine loadworker state属性(对于状态)上添加侦听器,可以在中查看loadworker解决方案的示例

导入javafx.concurrent.Worker.State;
. . .
webEngine.getLoadWorker().stateProperty().addListener(
新的ChangeListener(){
公共无效已更改(ObservalEvalue ov、State oldState、State newState){
if(newState==State.successed){
//在这里执行脚本。
}
}
});
webEngine.load(“http://javafx.com");

在浏览器中与firebug一起尝试js代码片段。我这样做了,效果很好。我认为问题是因为WebEngine的浏览器从超链接中删除了click事件,所以调用javascript click()函数将不起作用。
import javafx.concurrent.Worker.State;
. . .
webEngine.getLoadWorker().stateProperty().addListener(
        new ChangeListener<State>() {
            public void changed(ObservableValue ov, State oldState, State newState) {
                if (newState == State.SUCCEEDED) {
                    // execute script here . . . .
                }
            }
        });
webEngine.load("http://javafx.com");