我在这个网站上,我正在尝试填写字段并按提交: 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();
但是,这似乎不起作用。没有下载文件。 有什么想法吗?
答案 0 :(得分:1)
嗯,你几乎就在那里:
UnexpectedPage page3 = Button2.click();
// then copy from page3.getInputStream() to the preferred location
IOUtils.copy(page3.getInputStream(), new FileOutputStream("c:\\output.csv"));