qurikuduo · GitHub

This is what I do in my code using HtmlUnit 3.11.0 :

      try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX);) {
            try{
                webClient.getOptions().setJavaScriptEnabled(jsFlag);
                webClient.getOptions().setCssEnabled(cssFlag);
                webClient.getOptions().setUseInsecureSSL(true);
                webClient.setJavaScriptErrorListener(new SilentJavaScriptErrorListener());
                webClient.getOptions().setPrintContentOnFailingStatusCode(false);
                webClient.setCssErrorHandler(new SilentCssErrorHandler());
                webClient.setJavaScriptErrorListener(new SilentJavaScriptErrorListener());
                webClient.setAjaxController(new NicelyResynchronizingAjaxController());
                webClient.getOptions().setTimeout(25 * 1000);
                webClient.getOptions().setPopupBlockerEnabled(false);
                webClient.getOptions().setRedirectEnabled(true);
                webClient.getOptions().setUseInsecureSSL(true);
                webClient.getOptions().setThrowExceptionOnScriptError(false);
                webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
                webClient.addRequestHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4");
                webClient.getCookieManager().setCookiesEnabled(true);
                url = "http://www.jiyuan.gov.cn"
                HtmlPage page = null;
                page = webClient.getPage(url);
                // some other logic process such as extract Text from html etc.
             } catch (Exception e) {
                 logger.error(e);
             } finally {
                try {
                    webClient.getJavaScriptEngine().shutdown();
                    webClient.close();
                } catch (Exception e) {
                    logger.error("close webClient error", e);
                }
            }
       }

Then I got the error log as below:

Feb 20, 2024 10:59:32 am org.htmlunit.WebClient close
Exception while closing a TopLevelWindow
java.lang.NullPointerException
at org.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1303)
at org.htmlunit.html.HtmlPage.cleanUp(HtmlPage.java:372)
at org.htmlunit.TopLevelWindow.close(TopLevelWindow.java:144)
at org.htmlunit.WebClient.close(WebClient.java:2348)

Feb 20, 2024 10:59:32 am org.htmlunit.WebClient close
Sill 1 top level windows are open. Please report this error!

I don't know what the problem is. I'm trying to figure it out, but I haven't made any progress yet. Any suggestions would be hugely helpful. Thanks a lot.

Read the original on github.com ↗