2010-12-22 77 views
4

我有一個GWT應用程序,我嘗試索引。HtmlUnit&GWT錯誤

我使用的HtmlUnit來獲取生成的HTML內容:

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6); 
HtmlPage refDesing = webClient.getPage("http://localhost:8080/MyGWTApp/#page2"); 
FileOutputStream fos1 = new FileOutputStream("D:\\work\\out\\page2.html"); 
fos1.write(refDesing.asXml().getBytes()); 
fos1.close(); 

但我得到以下錯誤,返回的頁面大約空!

Dec 22, 2010 6:16:25 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 
WARNING: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://xxxxxxxxxxxx/xxxxxxxx/xxxxxxxx/xxxxxxxxxx.nocache.js', but got 'application/x-javascript'. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [485:24] Error in expression. Invalid token "=". Was expecting one of: <S>, <COMMA>, "/", <PLUS>, "-", <HASH>, <STRING>, ")", <URI>, "inherit", <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <DIMENSION>, <PERCENTAGE>, <NUMBER>, <FUNCTION>, <IDENT>. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [485:29] Error in style rule. Invalid token "\n". Was expecting one of: "}", ";". 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: null [485:29] Ignoring the following declarations in this rule. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [518:24] Error in expression. Invalid token "=". Was expecting one of: <S>, <COMMA>, "/", <PLUS>, "-", <HASH>, <STRING>, ")", <URI>, "inherit", <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <DIMENSION>, <PERCENTAGE>, <NUMBER>, <FUNCTION>, <IDENT>. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [518:29] Error in style rule. Invalid token "\n ". Was expecting one of: "}", ";". 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: null [518:29] Ignoring the following declarations in this rule. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [541:24] Error in expression. Invalid token "=". Was expecting one of: <S>, <COMMA>, "/", <PLUS>, "-", <HASH>, <STRING>, ")", <URI>, "inherit", <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <DIMENSION>, <PERCENTAGE>, <NUMBER>, <FUNCTION>, <IDENT>. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [541:29] Error in style rule. Invalid token "\n ". Was expecting one of: "}", ";". 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: null [541:29] Ignoring the following declarations in this rule. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [951:24] Error in expression. Invalid token "=". Was expecting one of: <S>, <COMMA>, "/", <PLUS>, "-", <HASH>, <STRING>, ")", <URI>, "inherit", <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <DIMENSION>, <PERCENTAGE>, <NUMBER>, <FUNCTION>, <IDENT>. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [951:29] Error in style rule. Invalid token "\n". Was expecting one of: "}", ";". 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: null [951:29] Ignoring the following declarations in this rule. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [977:24] Error in expression. Invalid token "=". Was expecting one of: <S>, <COMMA>, "/", <PLUS>, "-", <HASH>, <STRING>, ")", <URI>, "inherit", <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <DIMENSION>, <PERCENTAGE>, <NUMBER>, <FUNCTION>, <IDENT>. 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: null [977:29] Error in style rule. Invalid token "\n". Was expecting one of: "}", ";". 
Dec 22, 2010 6:16:27 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: null [977:29] Ignoring the following declarations in this rule. 

編輯:

我的意思大致爲空時,這裏是返回的HTML的快照:

請注意,不是顯示在原始頁面的所有數據(原始來自DB)由HtmlUnit返回。還有什麼「?」手段?我不認爲這意味着任何編碼錯誤導致所有字都是清晰的ASCII字符。

<td align="center" style="vertical-align: top;"> 
    <table class="refDesignGrid" cellspacing="5"> 
     <colgroup> 
     <col/> 
     </colgroup> 
     <tbody align="left"> 
     <tr> 
      <td align="left" style="vertical-align: top;"> 
      <table cellpadding="0" class="categoryItem" cellspacing="0"> 
       <tbody align="left"> 
       <tr> 
        <td align="left" style="vertical-align: top;"> 
        <div class="header4"> 
         C++ 
        </div> 
        </td> 
       </tr> 
       </tbody> 
      </table> 
      </td> 
      <td align="left" style="vertical-align: top;"> 
      <table cellpadding="0" class="categoryItem" cellspacing="0"> 
       <tbody align="left"> 
       <tr> 
        <td align="left" style="vertical-align: top;"> 
        <div class="header4"> 
         Java 
        </div> 
        </td> 
       </tr> 
       </tbody> 
      </table> 
      </td> 
      <td align="left"> 
      <table cellpadding="0" class="categoryItem" cellspacing="0"> 
       <tbody align="left"> 
       <tr> 
        <td align="left" style="vertical-align: top;"> 
        <div class="header4"> 
         C# 
        </div> 
        </td> 
       </tr> 
       </tbody> 
      </table> 
      </td> 
      <td> 
      ? 
      </td> 
     </tr> 
     <tr> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
     </tr> 
     <tr> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
     </tr> 
     <tr> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
     </tr> 
     <tr> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
      <td> 
      ? 
      </td> 
     </tr> 
     </tbody> 
    </table> 
    </td> 
</tr> 
</tbody> 
</table> 
</div> 

回答

1

答案就在這裏: http://htmlunit.sourceforge.net/faq.html#AJAXDoesNotWork

使用的HtmlUnit可完成執行主線程之前 讓後臺線程運行。您有幾個選項:

webClient.setAjaxController(new NicelyResynchronizingAjaxController());會告訴您的WebClient 實例重新同步異步XHR。 webClient.waitForBackgroundJavaScript(10000);或 webClient.waitForBackgroundJavaScriptStartingBefore(10000);在 之後獲取頁面並在操作之前。顯式等待您的JavaScript運行時預期滿足的 條件,例如 當註冊到WebClient的實例

//try 20 times to wait .5 second each for filling the page. 
for (int i = 0; i < 20; i++) { 
    if (condition_to_happen_after_js_execution) { 
     break; 
    } 
    synchronized (page) { 
     page.wait(500); 
    } 
} 
+0

雖然這可能在理論上回答這個問題,[這將是更可取的](http://meta.stackexchange.com/q/8259)在此包含答案的基本部分,提供鏈接供參考。 – 2011-10-27 14:56:02

2

HtmlUnit可能有點瑣碎,特別是可以讓事情看起來更糟糕。

創建這兩個類:

import org.w3c.css.sac.CSSException; 
import org.w3c.css.sac.CSSParseException; 
import com.gargoylesoftware.htmlunit.DefaultCssErrorHandler; 

/* 
* get rid of warnings... and provide a place to hang a break point 
*/ 
public class QuietCssErrorHandler 
    extends DefaultCssErrorHandler 
{ 

    @Override public void error(CSSParseException e) throws CSSException 
    { 
     super.error(e) ; 
    } 

    @Override public void fatalError(CSSParseException e) throws CSSException 
    { 
     super.fatalError(e) ; 
    } 

    @Override public void warning(CSSParseException e) throws CSSException 
    { 
    } 
} 

import com.gargoylesoftware.htmlunit.IncorrectnessListener; 

public class SilentIncorrectnessListener 
    implements IncorrectnessListener 
{ 
    @Override public void notify(String message, Object origin) 
    { 
     // do nuttin' honey! 
    } 
} 

那麼當您創建Web客戶端...

wc.setIncorrectnessListener(new SilentIncorrectnessListener()) ; 
wc.setCssErrorHandler(new QuietCssErrorHandler()) ; 

然後你應該得到更少的警告。

至於「大約爲空」......這是什麼意思?

+0

這些處理程序告訴我同樣的錯誤。請參閱我的關於「大約爲空」的編輯 – 2010-12-23 07:24:19