Question

J'ai une application GWT que j'essaie d'indexer.

J'utilise HtmlUnit pour obtenir le contenu du code HTML généré:

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();

Mais je reçois l'erreur suivante et la page environ revins vide!

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.

EDIT:

Qu'est-ce que je veux dire par est à peu près vide, photographie instantanée ici du code HTML retourné:

S'il vous plaît noter que, toutes les données affichées dans la page d'origine (qui a obtenu de DB d'origine) est retourné par HtmlUnit. Aussi ce que "?" veux dire? Je ne pense pas que cela signifie que toute erreur de codage cause de tous les mots sont des caractères ASCII clairs.

<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>
Était-ce utile?

La solution 2

La réponse est ici: http://htmlunit.sourceforge.net/faq.html#AJAXDoesNotWork

  

Le fil conducteur à l'aide HtmlUnit peut être avant de terminer l'exécution   fils de fond permettant de fonctionner. Vous avez deux options:

     

webClient.setAjaxController (nouveau   NicelyResynchronizingAjaxController ()); racontera votre WebClient   par exemple pour re-synchroniser XHR asynchrone.   webClient.waitForBackgroundJavaScript (10000); ou   webClient.waitForBackgroundJavaScriptStartingBefore (10000); juste après   obtenir la page et avant la manipuler. Explicitement attendre un   condition devrait être remplie lorsque votre court JavaScript,   par exemple.

//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);
     }
}

Autres conseils

HtmlUnit peut être un peu bavard, et en particulier peut rendre les choses pires que ce qu'ils sont.

Créer ces deux classes:

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 
    {
    }
}

et

import com.gargoylesoftware.htmlunit.IncorrectnessListener;

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

puis lorsque vous créez votre WebClient ...

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

Et vous devriez alors obtenir moins d'avertissements.

En ce qui concerne "environ vide" ... qu'est-ce que cela veut dire?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top