1

I cannot get the HTML Unit Driver warnings to turn off. The code below reduced the original amount of warnings. Cannot get the last bit to turn off.

I cannot get the HTML Unit Driver warnings to turn off. The code below reduced the original amount of warnings. Cannot get the last bit to turn off.

My code:

  LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

  java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
  java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
  java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
  java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 

  htmlDriver = new HtmlUnitDriver(browserVersion, isJavaScriptEnabled) {
    @Override
    protected WebClient modifyWebClient(WebClient client) {
      WebClient modifiedClient = super.modifyWebClient(client);

      modifiedClient.getOptions().setThrowExceptionOnScriptError(false); // see here
      modifiedClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
      modifiedClient.getOptions().setJavaScriptEnabled(isJavaScriptEnabled);
      modifiedClient.setCssErrorHandler(new SilentCssErrorHandler());
      modifiedClient.getOptions().setCssEnabled(false);
      modifiedClient.getOptions().setPrintContentOnFailingStatusCode(false);

      modifiedClient.getOptions().setTimeout(10000);

      modifiedClient.setIncorrectnessListener(new IncorrectnessListener() {

        @Override
        public void notify(String arg0, Object arg1) {
          // TODO Auto-generated method stub
        }
      });

      modifiedClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {
        @Override
        public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
          // TODO Auto-generated method stub
        }

        @Override
        public void scriptException(HtmlPage arg0, ScriptException arg1) {
          // TODO Auto-generated method stub
        }

        @Override
        public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
          // TODO Auto-generated method stub
        }

        @Override
        public void loadScriptError(HtmlPage page, java.net.URL scriptUrl,
                                    Exception exception) {
          // TODO Auto-generated method stub
        }
      });
      modifiedClient.setHTMLParserListener(new HTMLParserListener() {

        @Override
        public void error(String message, java.net.URL url, String html, int line, int column,
                          String key) {
          // TODO Auto-generated method stub
        }

        @Override
        public void warning(String message, java.net.URL url, String html, int line, int column,
                            String key) {
          // TODO Auto-generated method stub
        }
      });

      modifiedClient.setHTMLParserListener(new HTMLParserListener() {
        @Override
        public void error(String message, URL url, String html, int line, int column,
                          String key) {
          // TODO Auto-generated method stub
        }

        @Override
        public void warning(String message, URL url, String html, int line, int column,
                            String key) {
          // TODO Auto-generated method stub
        }
      });
      modifiedClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
      modifiedClient.getOptions().setThrowExceptionOnScriptError(false);


      return modifiedClient;
    }
  };

But I still get:

01:12:36.667 [Thread-10] ERROR c.g.h.javascript.StrictErrorReporter - runtimeError: message=[An invalid or illegal selector was specified (selector: 
01:12:37.264 [Thread-10] ERROR c.g.h.javascript.StrictErrorReporter - runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: :x).] sourceName=[http://code.jquery.com/jquery-1.9.1.js] line=[4224] lineSource=[null] lineOffset=[0]
01:12:38.393 [JS executor for com.gargoylesoftware.htmlunit.WebClient@4c085759] ERROR c.g.h.javascript.StrictErrorReporter - runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: :x).] sourceName=[http://code.jquery.com/jquery-1.9.1.js] line=[4224] lineSource=[null] lineOffset=[0]
01:12:41.251 [JS executor for com.gargoylesoftware.htmlunit.WebClient@4c085759] ERROR c.g.h.javascript.StrictErrorReporter - runtimeError: message=[An invalid or illegal selector was specified (selector: ':text' error: Invalid selector: :text).] sourceName=[http://code.jquery.com/jquery-1.9.1.js] line=[3983] lineSource=[null] lineOffset=[0]
01:12:41.256 [JS executor for com.gargoylesoftware.htmlunit.WebClient@4c085759] ERROR c.g.h.javascript.StrictErrorReporter - runtimeError: message=[An invalid or illegal selector was specified (selector: ':text' error: Invalid selector: :text).] sourceName=[http://code.jquery.com/jquery-1.9.1.js] line=[3983] lineSource=[null] lineOffset=[0]

How do you turn off these messages completely?

Febian Shah
  • 967
  • 5
  • 12
  • 23

1 Answers1

0

you need to Disable logging from logback XML this kind of deactivation

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

not working, I think they using locgback and it can't be disabled from the log4j config too

com.gargoylesoftware.htmlunit.level = WARNING
com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter.level = OFF 

you need something like that in your logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>
    <logger name="com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter" level="OFF" />
</configuration> 
zviad
  • 11
  • 1
  • Turning off logging just hides the actual problem and doesn't solve it. – Code-Apprentice Feb 01 '23 at 20:21
  • in that case, the task is turn off logging, you can't fix the javascript error for an external web page from the HTTP client, sometimes you just need to ignore them – zviad Feb 01 '23 at 20:46
  • ok, I see what you mean. I didn't look closely enough at what the code is doing and assumed these were errors from the OP's own code. – Code-Apprentice Feb 01 '23 at 22:06