5

我目前正在尝试 HtmlUnit。我想填写表格然后提交。但这会导致错误,这似乎是由无法访问的文件引起的,从而导致抛出连接重置。我该如何抑制这种情况,因为考虑到整个网站的加载过程,这并不重要?不过,它似乎不会影响程序,我只是希望它被静音。

Apr 05, 2012 12:25:13 PM com.gargoylesoftware.htmlunit.html.HtmlPage loadExternalJavaScriptFile
Schwerwiegend(severe): Error loading JavaScript from [*link*].
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:149)
    at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:110)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:264)
    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:281)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:645)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:776)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:152)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1439)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1358)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.loadJavaScriptFromUrl(HtmlPage.java:1008)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:950)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:349)
    at com.gargoylesoftware.htmlunit.html.HtmlScript$1.execute(HtmlScript.java:230)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:642)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.access$400(JavaScriptEngine.java:79)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:590)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:537)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:499)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:474)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:870)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:302)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:368)
    at com.gargoylesoftware.htmlunit.html.HtmlScript$1.execute(HtmlScript.java:230)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:240)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:598)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:556)
    at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1142)
    at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1044)
    at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:206)
    at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:329)
    at org.cyberneko.html.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3018)
    at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2005)
    at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:908)
    at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499)
    at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:789)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:225)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:179)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:221)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:106)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:433)
    at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2187)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:625)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.processPostponedActions(JavaScriptEngine.java:707)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1246)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1195)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1158)
    at com.gargoylesoftware.htmlunit.html.HtmlImageInput.click(HtmlImageInput.java:138)
    at com.gargoylesoftware.htmlunit.html.HtmlImageInput.click(HtmlImageInput.java:99)
    at com.tronicguard.discovery.WebsiteManipulatorImplementation.<init>(WebsiteManipulatorImplementation.java:71)
    at Main.main(Main.java:10)
4

7 回答 7

4

尝试以下

webClient.setThrowExceptionOnFailingStatusCode(false);

它应该处理 404(未找到)错误状态。

于 2012-04-05T11:23:21.580 回答
2

在使用 jkd1.7.0_3 编译后运行 Jenkins 的测试工具时,我遇到了类似的错误。回到 JDK1.6.0_30 解决了这个问题。

于 2012-07-13T08:46:31.243 回答
1

实例化 htmlUnit 驱动程序如下:

driver = new SilentHtmlUnitDriver(capabilities);

将以下内容添加为嵌套类(在您实例化驱动程序的同一类中):

         protected class SilentHtmlUnitDriver extends HtmlUnitDriver {
              SilentHtmlUnitDriver() {
                  super();
                  this.getWebClient().setCssErrorHandler(new SilentCssErrorHandler());
                  this.getWebClient().getOptions().setThrowExceptionOnFailingStatusCode(false);
                  this.getWebClient().getOptions().setThrowExceptionOnScriptError(false);

            }

            SilentHtmlUnitDriver(DesiredCapabilities capabilities) {
               super(capabilities);
               this.getWebClient().setCssErrorHandler(new SilentCssErrorHandler());
            }
}
于 2014-06-12T17:53:47.060 回答
1

试试这个来禁用 HtmlUnit 中烦人的警告(对不起,如果代码不太正确 - 我从 Clojure “翻译”了它,但我希望你至少能明白主要思想):

Import java.util.logging.Logger;
Import java.util.logging.Level;

Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 

查看关闭 HtmlUnit 警告以获取更多信息。那里有一个更复杂的解决方案,但这个简单的解决方案对我有用。

于 2014-06-17T05:57:39.957 回答
1

HtmlPage.loadExternalJavaScriptFile()将始终根据您的日志记录设置记录此错误。您需要禁用页面的日志记录,或更改阈值。

例如,如果你使用 log4j,你可以设置属性log4j.logger.com.gargoylesoftware.htmlunit.html.HtmlPage = FATAL

但是,这将隐藏HtmlPage中记录的所有错误的记录。

于 2012-04-10T23:44:34.193 回答
1

用这个:

LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
        java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF); 
        java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
于 2015-02-12T18:51:51.097 回答
0

您可以使用HttpUnitOptions.setScriptingEnabled(true);禁用HttpUnit中的 javascript 以及HttpUnitOptions.setExceptionsThrownOnScriptError(false);

于 2012-04-05T12:04:08.167 回答