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

博主在使用SWT Browser类时遇到一个问题,尝试获取动态加载的ASP页面源代码,但浏览器事件监听器的完成事件在内容完全显示之前触发。他们希望通过JavaScript检查页面是否完全加载。提供的解决方案是使用window.onload事件,等待所有资源加载后再执行代码,例如弹出警告框。然而,这种方法在某些场景下可能不奏效,如使用浏览器的前进和后退按钮。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值