javaFX webengine修复getDocument在WebEngine Succes上返回null

时间:2015-12-24 15:52:23

标签: javafx javafx-webengine

我正在为java中的大学项目开发​​一个网络爬虫。 我在名为Loader的Object中使用WebEngine。我用它来下载trhought webengine的页面,用WebEngine方法getDocument()获取Document(org.w3c.dom.document),使用那个文件在JavaFX线程里面生成一个解析树(因为WebEngine文件不能用在javaFX线程之外)。所有这些操作都在WebEngine Worker的侦听器内。我也正在捕获worker的Failed和Cancelled状态以生成null解析树。

有时(这不是确定性的)当我向webEngine提交一个VALID URL时,它的worker进入成功状态,但是getDocument方法返回NULL。现在我正抓住这个特殊情况,将其视为失败。

有一种方法可以在java标准库中修复此问题吗? 或者是底层WebKit实现中的问题? P.S webengine作为AtomicReference访问。 代码:在加载(URL网址)方法

       Platform.runLater(() -> we.get().load(url.toString()));
       //Wait for the listenr to be executed
       Platform.runLater(() -> we.get().load("")); //To Avoid
      submitting the same page twice casung the webengine to be stuck in Running state
        return Result(url, parsing, exception); //It's more complex that this but it's not this that matter

在听众中:

we.get().getLoadWorker().stateProperty().addListener((o, ov, nv) -> {
            if (nv == Worker.State.SUCCEEDED || nv == Worker.State.FAILED || nv == Worker.State.CANCELLED) {
                if(!we.get().getLocation().equalsIgnoreCase("")  && !we.get().getLocation().equalsIgnoreCase("about:blank")) {
                    if (nv == Worker.State.SUCCEEDED) {
                        org.w3c.dom.Document dom = we.get().getDocument();
                        if (dom != null)
                            parser = new Parsing(we.get().getDocument());
                        else
                            nv = Worker.State.FAILED;
                            //THIS: Why on webengine loading completition
                            //the GetDocument() return null, in the  place of the actual document?
                    }
                    downloadRes = nv;
                }
            }
        });

编辑:显式化setJavaScriptEnabled(false)几乎解决了这个问题。

0 个答案:

没有答案