HTMLUnit doesn't wait for Javascript

后端 未结 4 542
面向向阳花
面向向阳花 2020-11-27 18:09

I have a GWT based page that I would like to create an HTML snapshot for it using HtmlUnit. The page loads using Ajax/JavaScript information on a product, so for about 1 sec

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 18:46

    As documentation states, waitForBackgroundJavaScript is experimental:

    Experimental API: May be changed in next release and may not yet work perfectly!

    The next approach has always worked for me, regardless of the BrowserVersion used:

    int tries = 5;  // Amount of tries to avoid infinite loop
    while (tries > 0 && aCondition) {
        tries--;
        synchronized(page) {
            page.wait(2000);  // How often to check
        }
    }
    

    Note aCondition is whatever you're checking for. EG:

    page.getElementById("loading-text-element").asText().equals("Loading...")
    

提交回复
热议问题