如何忽略与 jQuery 相关的 HTMLUnit 警告/错误?

发布于 2024-10-20 17:40:09 字数 333 浏览 2 评论 0原文

是否可以教 HTMLUnit 忽略网页上的某些 javascript 脚本/文件?其中一些超出了我的控制范围(例如 jQuery),我无法对它们做任何事情。警告很烦人,例如:

[WARN] com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument:
getElementById(script1299254732492) did a getElementByName for Internet Explorer

实际上我正在使用 JSFUnit,而 HTMLUnit 在它下工作。

Is it possible to teach HTMLUnit to ignore certain javascript scripts/files on a web page? Some of them are just out of my control (like jQuery) and I can't do anything with them. Warnings are annoying, for example:

[WARN] com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument:
getElementById(script1299254732492) did a getElementByName for Internet Explorer

Actually I'm using JSFUnit and HTMLUnit works under it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疑心病 2024-10-27 17:40:09

如果您想避免由于任何 JavaScript 错误而出现异常:

 webClient.setThrowExceptionOnScriptError(false);        

If you want to avoid exceptions because of any JavaScript errors:

 webClient.setThrowExceptionOnScriptError(false);        
何以畏孤独 2024-10-27 17:40:09

好吧,我还没有找到办法,但我已经找到了有效的解决方法。尝试实现FalsifyingWebConnection。请看下面的示例代码。

public class PinConnectionWrapper extends FalsifyingWebConnection {

    public PinConnectionWrapper(WebClient webClient)
            throws IllegalArgumentException {
        super(webClient);
    }

    @Override
    public WebResponse getResponse(WebRequest request) throws IOException {
        WebResponse res = super.getResponse(request);
        if(res.getWebRequest().getUrl().toString().endsWith("/toolbar.js")) {
            return createWebResponse(res.getWebRequest(), "",
"application/javascript", 200, "Ok");
        }
        return res;
    }

}

在上面的代码中,每当 HtmlUnit 请求toolbar.js 时,我的代码将简单地返回一个假的空响应。您可以将上面的包装类插入到 HtmlUnit 中,如下所示。

final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
new PinConnectionWrapper(webClient);

Well I am yet to find a way for that but I have found an effective workaround. Try implementing FalsifyingWebConnection. Look at the example code below.

public class PinConnectionWrapper extends FalsifyingWebConnection {

    public PinConnectionWrapper(WebClient webClient)
            throws IllegalArgumentException {
        super(webClient);
    }

    @Override
    public WebResponse getResponse(WebRequest request) throws IOException {
        WebResponse res = super.getResponse(request);
        if(res.getWebRequest().getUrl().toString().endsWith("/toolbar.js")) {
            return createWebResponse(res.getWebRequest(), "",
"application/javascript", 200, "Ok");
        }
        return res;
    }

}

In the above code whenever HtmlUnit will request for toolbar.js my code will simply return a fake empty response. You can plug-in your above wrapper class into HtmlUnit as below.

final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
new PinConnectionWrapper(webClient);
但可醉心 2024-10-27 17:40:09

看一下 WebClient .setScriptPreProcessor。它将使您有机会在执行之前修改(或者在您的情况下,停止)给定的脚本。

另外,如果只是警告让您感到不安,我建议更改日志级别

Take a look at WebClient.setScriptPreProcessor. It will give you the opportunity to modify (or in your case, stop) a given script before it is executed.

Also, if it is just the warnings getting on your nerves I would suggest changing the log level.

眼眸里的那抹悲凉 2024-10-27 17:40:09

如果您有兴趣忽略所有警告日志条目,可以将 log4j.properties 文件中 com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument 的日志级别设置为 INFO。

If you are interested in ignoring all warning log entries you can set the log level to INFO for com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument in the log4j.properties file.

み格子的夏天 2024-10-27 17:40:09

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);

插入此代码。

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);

Insert this code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文