HtmlUnit从href调用javascript来下载文件 [英] HtmlUnit to invoke javascript from href to download a file
问题描述
我试图下载一个似乎必须点击浏览器的文件。该网站使用一个表单,其中内部是几个hrefs到一个名为downloadFile的javascript函数。在这个函数中,名为poslimit的元素由document.getElementById获取:
I have tried to download a file that seems to have to be clicked vi a browser. The site uses a form for which inside are several hrefs to a javascript function named downloadFile. In this function, the element named poslimit is obtained by document.getElementById:
function downloadFile(actionUrl, formId)
{
document.getElementById(formId).action=actionUrl;
document.getElementById(formId).submit();
}
HTML源代码片段:
The HTML source snippett:
<form method="post" name="commandForm" action="position-limits" id="poslimit">
<div id="content">
<li><a href="javascript:downloadFile('position-limits?fileName=20130711&positionLimit=CURRENT_POSITION_LIMIT_', 'poslimit');" > July 11, 2013 </a></li>
所以在href中点击上面链接的代码会调用另一个文件中的javascript:
So clicking on the linked code above in the href invokes the javascript in another file:
我尝试过:
WebClient webClient = new WebClient(BrowserVersion.CHROME_16);
HtmlPage page = webClient.getPage("http://www.theocc.com/webapps/position-limits");
HtmlForm elt = page.getHtmlElementById("poslimit");
elt.setAttribute("action", "position-limits?fileName=20130709&positionLimit=POSITIONLIMITCHANGE_");
InputStream is = elt.click().getWebResponse().getContentAsStream();
int b = 0;
while ((b = is.read()) != -1)
{
System.out.print((char)b);
}
webClient.closeAllWindows();
还尝试使用HtmlElement
我也尝试过:
Also tried using HtmlElement I Also tried:
WebClient webClient = new WebClient(BrowserVersion.CHROME_16);
HtmlPage page = webClient.getPage("http://www.theocc.com/webapps/position-limits");
ScriptResult sr = page.executeJavaScript("downloadFile('position-limits?fileName=20130709&positionLimit=POSITIONLIMITCHANGE_', 'poslimit'");
InputStream is = sr.getNewPage().getWebResponse().getContentAsStream();
int b = 0;
while ((b = is.read()) != -1)
{
System.out.print((char)b);
}
webClient.closeAllWindows();
这些来自这个和其他板上的例子,但是我继续只是将原始页面提回而不是附加的文件。我也想知道我是否需要查看正确的页面响应的历史,也许返回窗口/文档
Both of these come from examples on this and other boards, but I continue to just get the original page back instead of the attached file. I am also wondering if I need to look at history for the proper page response as maybe the return window/document I need is the previous. Courteous links to full explanations or good exampled documentaion as well as source I could try are appreciated.
推荐答案
我们需要的是以前的完整解释或良好的示例文档以及源代码可以尝试的礼貌链接。所以我认为这可能对他人有帮助,因为我还没有看到一个工作的例子。
So I think this may be helpful to others as I have not seen a working example.
WebClient webClient = new WebClient(BrowserVersion.CHROME_16);
HtmlPage page = webClient.getPage("http://www.theocc.com/webapps/position-limits");
HtmlAnchor anchor = null;
List<HtmlAnchor> anchors = page.getAnchors();
for (int i = 0; i < anchors.size(); ++i)
{
anchor = anchors.get(i);
String sAnchor = anchor.asText();
// This date should come in from args
if (sAnchor.equals("July 9, 2013"))
break;
}
// This is not safe, need null check
Page p = anchor.click();
InputStream is = p.getWebResponse().getContentAsStream();
int b = 0;
while ((b = is.read()) != -1)
{
System.out.print((char)b);
}
webClient.closeAllWindows();
这个问题帮助我一点点,因为我尝试了锚点,它的工作。 努力点击htmlunit中的链接
This question helped me a bit as I tried the anchor thingy and it worked. struggling to click on link within htmlunit
这篇关于HtmlUnit从href调用javascript来下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!