如何忽略与 jQuery 相关的 HTMLUnit 警告/错误?
是否可以教 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想避免由于任何 JavaScript 错误而出现异常:
If you want to avoid exceptions because of any JavaScript errors:
好吧,我还没有找到办法,但我已经找到了有效的解决方法。尝试实现
FalsifyingWebConnection
。请看下面的示例代码。在上面的代码中,每当 HtmlUnit 请求toolbar.js 时,我的代码将简单地返回一个假的空响应。您可以将上面的包装类插入到 HtmlUnit 中,如下所示。
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.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.
看一下 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.
如果您有兴趣忽略所有警告日志条目,可以将 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.
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.