且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java的SWT浏览器:等到动态页面完全加载

更新时间:2023-11-21 17:32:22

 的window.onload =函数(){
    警报(一切装)
}

的onload 等待,直到所有的资源被加载并会执行包含code,即启动警报。

请注意但是,的onload 可能无法在某些情况下工作。如果我没有记错,有时不的你工作使用浏览器的前进和后退按钮。

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

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.