我在Java中遇到了HtmlUnit这个奇怪的问题。我用它来从网站下载一些数据,过程是这样的:
1 - 登录
2 - 对于每个元素(汽车)
----- 3搜索汽车
----- 4从链接下载zip文件
代码:
创建webclient:
webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setJavaScriptEnabled(true);
webClient.setThrowExceptionOnScriptError(false);
DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
provider.addCredentials(USERNAME, PASSWORD);
webClient.setCredentialsProvider(provider);
webClient.setRefreshHandler(new ImmediateRefreshHandler());
登录:
public void login() throws IOException
{
page = (HtmlPage) webClient.getPage(URL);
HtmlForm form = page.getFormByName("formLogin");
String user = USERNAME;
String password = PASSWORD;
// Enter login and password
form.getInputByName("LoginSteps$UserName").setValueAttribute(user);
form.getInputByName("LoginSteps$Password").setValueAttribute(password);
// Click Login Button
page = (HtmlPage) form.getInputByName("LoginSteps$LoginButton").click();
webClient.waitForBackgroundJavaScript(3000);
// Click on Campa area
HtmlAnchor link = (HtmlAnchor) page.getElementById("ctl00_linkCampaNoiH");
page = (HtmlPage) link.click();
webClient.waitForBackgroundJavaScript(3000);
System.out.println(page.asText());
}
在网站搜索汽车:
private void searchCar(String _regNumber) throws IOException
{
// Open search window
page = page.getElementById("search_gridCampaNoi").click();
webClient.waitForBackgroundJavaScript(3000);
// Write plate number
HtmlInput element = (HtmlInput) page.getElementById("jqg1");
element.setValueAttribute(_regNumber);
webClient.waitForBackgroundJavaScript(3000);
// Click on search
HtmlAnchor anchor = (HtmlAnchor) page.getByXPath("//*[@id=\"fbox_gridCampaNoi_search\"]").get(0);
page = anchor.click();
webClient.waitForBackgroundJavaScript(3000);
System.out.println(page.asText());
}
下载pdf:
try
{
InputStream is = _link.click().getWebResponse().getContentAsStream();
File path = new File(new File(DOWNLOAD_PATH), _regNumber);
if (!path.exists())
{
path.mkdir();
}
writeToFile(is, new File(path, _regNumber + "_pdfs.zip"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
问题:
第一辆车工作正常,pdf已下载,但是当我搜索新车时,当我到达这条线时:
page = page.getElementById("search_gridCampaNoi").click();
我得到了这个例外:
Exception in thread "main" java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage
经过调试,我意识到我打电话的那一刻:
InputStream is = _link.click().getWebResponse().getContentAsStream();
返回类型page.getElementById(“search_gridCampaNoi”)的。点击()从HtmlPage到WebResponse的变化,所以不是接受了新的一页,我又收到,我已经下载的文件。
调试器的几个屏幕截图显示了这种情况:
第一次调用,返回类型OK:
第二次调用,返回类型已更改,我不再收到HtmlPage:
提前致谢!
答案 0 :(得分:9)
万一有人遇到同样的问题,我找到了一个解决方法。改变这一行:
InputStream is = _link.click().getWebResponse().getContentAsStream();
到
InputStream is = _link.openLinkInNewWindow().getWebResponse().getContentAsStream();
似乎可以解决问题。我现在在进行多次迭代时遇到问题,有时它可以工作,有时它不会,但至少我现在有一些东西。