我需要使用 htmlUnit webDriver 下载不同页面中的不同文件。我在页面上循环,对于每个页面,我都单击输入字段以触发下载。对于第一次下载,我单击一个 HtmlInputElement(一个提交按钮),它会将文件返回给我。之后,HtmlPage 变为 UnexpectedPage。检查 UnexcpetedPage 中的 WebResponse 我仍然发现文件已经下载。如何解决?
代码: [...]
public void test(HtmlPage htmlPage){
List<?> checkBox = htmlPage.getByXPath("//tbody[@id='TestID:ricerca_report:tb']/tr[child::td[contains(text(),'TEST')]]/td[1]/input[@type='checkbox']");
for (int j = 0; j < checkBox.size(); j++) {
HtmlCheckBoxInput checked = (HtmlCheckBoxInput) checkBox.get(j);
checked.setChecked(true);
}
writeResponseHtml(htmlPage, spOp);
InputStream input = null;
try{
HtmlSubmitInput htmlSubmitInput = htmlPage.getElementByName(param.getNameAttribute());
WebResponse webResponse = htmlSubmitInput.click().getWebResponse();
extractOriginalFileName(webResponse, spiderInfo.getFileName());
if( checkAndSaveFileExtension(webResponse) != null ){
input = webResponse.getContentAsStream();
downloadFile( input );
spiderInfo.setFileName("work/output/spider_logs/");
}else
spiderInfo.setSpiderState(SpiderStates.DOWNLOAD_ERROR);
}catch ( Exception e ){
logger.error("getOperation download exception; " + spOp + "; Exception: " + e);
spiderInfo.setSpiderState(SpiderStates.DOWNLOAD_ERROR);
page = null;
}
writeResponseHtml(htmlPage, spOp);
HtmlTableDataCell tableCell = htmlPage.getFirstByXPath(param.getContainsElement2());
Page p = tableCell.click(); // Here i got the exception
}
[....]