2016-01-24 209 views
1

这是我的代码。我尝试使用Java中的WebClient加载twitter注册页面。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。我拿到了手机版网页。如何解决这个问题呢?

янв 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 ======== 

回答

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

它如何帮助我吗? –

+0

此代码将关闭由htmlunit引起的所有javascript警告。这些警告是由于htmlunit库,而不是你的代码,你应该为你的页面获得正确的内容。 – haihui