gpt4 book ai didi

java - 为什么我的文件没有下载 (HTMLUnit) Java

转载 作者:行者123 更新时间:2023-11-30 06:35:37 24 4
gpt4 key购买 nike

我在这个网站上,我正在尝试填写字段并按提交: http://www.hmdb.ca/spectra/ms/search

点击提交后,我尝试将结果下载为 CSV(有一个按钮)。

我尝试的解决方案是(s、r、t、z 是字符串输入):

public static String hmdb() throws IOException {
WebClient webClient = new WebClient(BrowserVersion.CHROME);
Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
webClient.getOptions().setThrowExceptionOnScriptError(false);
HtmlPage page = webClient.getPage("http://www.hmdb.ca/spectra/ms/search");

HtmlTextArea searchBox = page.getElementByName("query_masses");
searchBox.setText(s);

HtmlSelect select = (HtmlSelect) page.getElementByName("ms_search_ion_mode");
HtmlOption option = select.getOptionByValue(r);
select.setSelectedAttribute(option, true);

HtmlInput searchBox2 = page.getElementByName("tolerance");
searchBox2.setValueAttribute(t);

HtmlSelect select2 = (HtmlSelect) page.getElementById("tolerance_units");
HtmlOption option2 = select2.getOptionByValue(z);
select2.setSelectedAttribute(option2, true);

HtmlSubmitInput Button = page.getFirstByXPath("//input[@value='Search'][@name='commit']");
HtmlPage pageafter=Button.click();

HtmlSubmitInput Button2 = pageafter.getFirstByXPath("//input[@value='Download Results As CSV'][@name='commit']");
Button2.click();

但是,这似乎不起作用。没有文件正在下载。有什么想法吗?

最佳答案

好吧,你就快到了:

UnexpectedPage page3 = Button2.click();

// then copy from page3.getInputStream() to the preferred location

IOUtils.copy(page3.getInputStream(), new FileOutputStream("c:\\output.csv"));

关于java - 为什么我的文件没有下载 (HTMLUnit) Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45246930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com