通过HtmlUnit下载CSV并读入数组

huangapple go评论86阅读模式
英文:

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&#39;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&#39;s not quite what I need - mostly because they weren&#39;t downloading a CSV and using the data in it.
I&#39;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&#39;t know where the file is downloading if it is at all.
Here&#39;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 &quot;save as CSV&quot; 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&#39;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&#39;t use the same behavior next time the method is called...
}
}
});

huangapple
  • 本文由 发表于 2020年10月7日 04:11:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64233168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定