通过 HtmlUnit 下载 CSV 并读入数组
Download CSV through HtmlUnit and read into array
我有一个页面有一个按钮可以下载 CSV 文件格式的信息。该按钮会打开确认对话框以下载文件。我需要将该文件存储到一个临时位置(无论是内存还是保存到实际文件然后删除它),然后将 CSV 中的数据读取到数组中。
我已经尝试了这些问题中的代码 (question 1, question 2, question 3, and ),但这并不是我所需要的——主要是因为他们没有下载 CSV 文件并使用其中的数据。
我不确定是否打开了 ConfirmDialog,但我确实添加了 ConfirmHandler
返回 true
以尝试下载文件。但是,我根本不知道文件在哪里下载。
这是我发生的事情以及我卡住的地方:
我登录就好了。我去报告生成器。我生成了一个在新 window 中打开的报告。新的 window 打开很好,我用 WebWindowListener
抓住了它。然后我在新 window 上搜索“另存为 CSV”按钮。我可以找到它,我可以点击它,但是 System.out.print
调用显示 ConfirmHandler
没有触发。
for (DomElement e : newPage.getElementsByTagName("button")) {
int i = 0;
webClient.setConfirmHandler(new ConfirmHandler() {
private static final long serialVersionUID = 1L;
@Override
public boolean handleConfirm(Page arg0, String arg1) {
System.out.println("Test"); //isn't firing
return false;
}
});
if (((HtmlButton) e).getAttribute("onclick").contains("CSV")) {
((HtmlButton) e).click();
}else {
if (i++ == (newPage.getElementsByTagName("button").size() - 1)) throw new AssertionError("CSV button not found");
}
}
这个答案的灵感来自this answer。
我真的很想在不下载文件的情况下获取 CSV 信息,所以我在 webWindowListener
的 webWindowContentChanged(arg0)
方法中,只使用 arg0.getWebWindow().getEnclosingPage().getWebResponse().getContentAsString()
然后使用 String.split()
几次得到我想要的信息。
代码如下所示,这样更清晰一些:
webClient.addWebWindowListener(new WebWindowListener() {
@Override
public void webWindowContentChanged(WebWindowEvent arg0) {
if (CSVclicked) { //boolean that is set true when I click the download button...
String CSV = arg0.getWebWindow().getEnclosedPage().getWebResponse().getContentAsString();
//do things...
CSVclicked = false; //don't use the same behavior next time the method is called...
}
}
});
我有一个页面有一个按钮可以下载 CSV 文件格式的信息。该按钮会打开确认对话框以下载文件。我需要将该文件存储到一个临时位置(无论是内存还是保存到实际文件然后删除它),然后将 CSV 中的数据读取到数组中。
我已经尝试了这些问题中的代码 (question 1, question 2, question 3, and
我不确定是否打开了 ConfirmDialog,但我确实添加了 ConfirmHandler
返回 true
以尝试下载文件。但是,我根本不知道文件在哪里下载。
这是我发生的事情以及我卡住的地方:
我登录就好了。我去报告生成器。我生成了一个在新 window 中打开的报告。新的 window 打开很好,我用 WebWindowListener
抓住了它。然后我在新 window 上搜索“另存为 CSV”按钮。我可以找到它,我可以点击它,但是 System.out.print
调用显示 ConfirmHandler
没有触发。
for (DomElement e : newPage.getElementsByTagName("button")) {
int i = 0;
webClient.setConfirmHandler(new ConfirmHandler() {
private static final long serialVersionUID = 1L;
@Override
public boolean handleConfirm(Page arg0, String arg1) {
System.out.println("Test"); //isn't firing
return false;
}
});
if (((HtmlButton) e).getAttribute("onclick").contains("CSV")) {
((HtmlButton) e).click();
}else {
if (i++ == (newPage.getElementsByTagName("button").size() - 1)) throw new AssertionError("CSV button not found");
}
}
这个答案的灵感来自this answer。
我真的很想在不下载文件的情况下获取 CSV 信息,所以我在 webWindowListener
的 webWindowContentChanged(arg0)
方法中,只使用 arg0.getWebWindow().getEnclosingPage().getWebResponse().getContentAsString()
然后使用 String.split()
几次得到我想要的信息。
代码如下所示,这样更清晰一些:
webClient.addWebWindowListener(new WebWindowListener() {
@Override
public void webWindowContentChanged(WebWindowEvent arg0) {
if (CSVclicked) { //boolean that is set true when I click the download button...
String CSV = arg0.getWebWindow().getEnclosedPage().getWebResponse().getContentAsString();
//do things...
CSVclicked = false; //don't use the same behavior next time the method is called...
}
}
});