Java SWT browser: Waiting till dynamic page is fully loaded

血红的双手。 提交于 2019-12-24 02:44:04

问题


I'm working with SWT browser class and I have a problem. I'm trying to copy page source from a page in which most of the data is dynamically loaded with use of ASP. The problem is that the event completed of browser event listener is of course fired before the whole page content is displayed. Here is a part of my code:

class GermaniaProgressListener implements ProgressListener {

  final Browser browser;
  Calendar calen;
  SimpleDateFormat dateFormat;
  static Integer counter;
  static Integer changeCounter;
  static Integer completeCounter;


  public GermaniaProgressListener(Browser browser, Calendar calen, SimpleDateFormat dateForm) {
    super();
    this.browser = browser;
    this.calen = calen;
    this.dateFormat = dateForm;
    counter = 1;
  }

  public void changed(ProgressEvent event) {

  }

  @Override
  public void completed(ProgressEvent arg0) {
    String pageContent = "";
    if(counter > 1) {
      try {
        pageContent = (String)browser.evaluate("return document.body.innerHTML");
        FileUtilities.writeFile(
            FilePaths.GERMANIA_HTML.getFilePath().replace(".", ((Integer)(counter-1)).toString() + "."),
            pageContent);
      } catch (IOException e) {
        e.printStackTrace();
        LoggingUtilities.logException(e);
        BrowserUtilities.closeBrowser(browser);
      }
    }
    if(counter < 8) {
      String strDate = dateFormat.format(calen.getTime());
      Boolean ret = browser.execute("this.controler.loadPage(0, '', 'd=" + strDate + "')");
      if(!ret) {
        LoggingUtilities.logString("Error getting germania pages");
        BrowserUtilities.closeBrowser(browser);
        return;
      }
      calen.add(Calendar.DATE, 1);
      counter++;
    } else {
      BrowserUtilities.closeBrowser(browser);
    }
  }
};

public class PageDowGermania {
  static public void getPageContent() {
    final Browser browser = BrowserUtilities.createBrowser();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    Calendar calen = Calendar.getInstance();
    calen.setTime(new Date());
    GermaniaProgressListener fpl = new GermaniaProgressListener(browser, calen, dateFormat);
    browser.addProgressListener(fpl);
    BrowserUtilities.startBrowser(browser, "http://www.germaniasport.hr");    
  }
}

I'm not an expert in dynamic web pages so maybe someone could help me if there is any possibility for example a script in JavaScript that could check it the page is fully loaded.

Thanks in advance


回答1:


window.onload = function () {
    alert("Everything loaded")
}

onload waits until all resources are loaded and will the execute the contained code, i.e. start an alert.

Note however, that onload might not work in some scenarios. If I remember correctly, it sometimes doesn't work of you use the back and forward buttons of the browser.



来源:https://stackoverflow.com/questions/12735755/java-swt-browser-waiting-till-dynamic-page-is-fully-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!