WebClient无法加载twitter页面

时间:2016-01-24 14:05:17

标签: javascript java webclient htmlunit

这是我的代码。我尝试用Java中的WebClient加载twitter注册页面。

public LoadTwitter() {
    client = new WebClient(BrowserVersion.CHROME);
    client.getOptions().setJavaScriptEnabled(true);
    client.getOptions().setThrowExceptionOnScriptError(false);
    //client.waitForBackgroundJavaScript(2000);
}

public void loadPage() throws IOException {
    HtmlPage page = client.getPage("http://twitter.com/signup");
}

但我有很多javascript错误。如果我关闭javascript。我得到网络的移动版本insteas。如何解决这个问题?

янв 24, 2016 6:50:27 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
янв 24, 2016 6:50:27 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
янв 24, 2016 6:50:28 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
янв 24, 2016 6:50:29 PM com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet isValidSelector
WARNING: Unhandled CSS condition type '7'. Accepting it silently.
янв 24, 2016 6:50:29 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: *:x).] sourceName=[https://abs.twimg.com/c/swift/en/init.c624198d27d7d00677cf16d484b32903b375bbc6.js] line=[108] lineSource=[null] lineOffset=[0]
янв 24, 2016 6:50:32 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
======= EXCEPTION START ========
EcmaError: lineNumber=[8] column=[0] lineSource=[null] name=[TypeError] sourceName=[https://abs.twimg.com/c/swift/en/bundle/signup.5fb6bc11a86475b13235f34876f4d57d28669252.js] message=[TypeError: Cannot call method "call" of undefined (https://abs.twimg.com/c/swift/en/bundle/signup.5fb6bc11a86475b13235f34876f4d57d28669252.js#8)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot call method "call" of undefined (https://abs.twimg.com/c/swift/en/bundle/signup.5fb6bc11a86475b13235f34876f4d57d28669252.js#8)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:865)
== CALLING JAVASCRIPT ==
  function (b) {
      b = a.event || b;
      if (b.type == "load" || w(["loaded", "complete"], this.readyState) > -1) {
          this.onreadystatechange = null;
          c.loaded();
      }
  }
======= EXCEPTION END ========

1 个答案:

答案 0 :(得分:0)

试试这个,它应该通过HtmlUnit删除日志:

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

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

client = new WebClient(BrowserVersion.CHROME);
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);