英文:
Download CSV through HtmlUnit and read into array
问题
我有一个页面,页面上有一个按钮,用于以CSV文件格式下载信息。单击该按钮会打开一个确认对话框以下载文件。我需要将该文件存储到临时位置(无论是存储在内存中还是保存到实际文件,然后在删除之后),然后将CSV中的数据读取到一个数组中。
我尝试过以下问题中的代码([问题1](https://stackoverflow.com/questions/12997871/download-file-using-htmlunit),[问题2](https://stackoverflow.com/questions/8049947/htmlunit-cant-retrieve-page-after-downloading-a-file?rq=1),[问题3](https://stackoverflow.com/questions/25136439/htmlunit-download-file)和[问题4](https://stackoverflow.com/questions/46058444/htmlunit-set-download-location)),但这并不完全符合我的需求 - 主要是因为它们没有下载CSV文件并使用其中的数据。
我不确定确认对话框是否已被打开,但我确实添加了一个返回`true`的`ConfirmHandler`来尝试下载文件。然而,如果文件被下载了,我不知道它下载到哪里。
以下是我的操作步骤以及遇到问题的地方:
我可以成功登录。我进入一个报表生成器。我生成一个在新窗口中打开的报表。新窗口成功打开,并且我使用`WebWindowListener`捕获它。然后,我在新窗口中搜索“另存为CSV”按钮。我可以找到该按钮,并且可以单击它,但是通过`System.out.print`的调用显示`ConfirmHandler`没有触发。
```java
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"); //未触发
return false;
}
});
if (((HtmlButton) e).getAttribute("onclick").contains("CSV")) {
((HtmlButton) e).click();
} else {
if (i++ == (newPage.getElementsByTagName("button").size() - 1)) throw new AssertionError("CSV按钮未找到");
}
}
<details>
<summary>英文:</summary>
I have a page that has a button to download information in the format of a CSV file. The button opens a confirm dialog to download the file. I need to store that file to a temporary location (whether that be memory or saving to an actual file and then deleting it after) and then read the data in the CSV to an array.
I've tried the code in these questions ([question 1](https://stackoverflow.com/questions/12997871/download-file-using-htmlunit), [question 2](https://stackoverflow.com/questions/8049947/htmlunit-cant-retrieve-page-after-downloading-a-file?rq=1), [question 3](https://stackoverflow.com/questions/25136439/htmlunit-download-file), and [question 4](https://stackoverflow.com/questions/46058444/htmlunit-set-download-location)), and it's not quite what I need - mostly because they weren't downloading a CSV and using the data in it.
I'm not sure that the ConfirmDialog is being opened, but I did add a `ConfirmHandler` returning `true` to attempt to download the file. However, I don't know where the file is downloading if it is at all.
Here's what I have happening and where I get stuck:
I log in just fine. I go to a report generator. I generate a report that opens in a new window. The new window opens fine and I catch it with a `WebWindowListener`. I then search for the "save as CSV" button on the new window. I can find that, and I can click on it, but a `System.out.print` call shows that the `ConfirmHandler` isn't firing.
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");
}
}
</details>
# 答案1
**得分**: 0
以下是翻译好的部分:
这个答案的灵感来自于[这个答案](https://stackoverflow.com/a/13021720/5900977)。
我真的想要获取CSV文件的信息,而不需要下载文件,所以我在我的`webWindowListener`的`webWindowContentChanged(arg0)`方法中,只是使用了`arg0.getWebWindow().getEnclosingPage().getWebResponse().getContentAsString()`,然后使用了`String.split()`几次以获取我想要的信息。
以下是代码示例,以便更清晰一些:
```java
webClient.addWebWindowListener(new WebWindowListener() {
@Override
public void webWindowContentChanged(WebWindowEvent arg0) {
if (CSVclicked) { // 当我点击下载按钮时设置为true...
String CSV = arg0.getWebWindow().getEnclosedPage().getWebResponse().getContentAsString();
// 进行一些操作...
CSVclicked = false; // 下次调用该方法时不要使用相同的行为...
}
}
});
英文:
The inspiration for this answer came from this answer.
I really wanted to get the CSV information without downloading the file, and so I, inside of the webWindowContentChanged(arg0)
method of my webWindowListener
, just used arg0.getWebWindow().getEnclosingPage().getWebResponse().getContentAsString()
and then used String.split()
a couple times to get the information I wanted.
Here's what the code looks like so it's a little clearer:
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...
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论