1

ブラウザ経由でクリックする必要があると思われるファイルをダウンロードしようとしました。このサイトでは、downloadFile という名前の JavaScript 関数へのいくつかの href が内部にあるフォームを使用しています。この関数では、poslimit という名前の要素が document.getElementById によって取得されます。

function downloadFile(actionUrl, formId)
{
    document.getElementById(formId).action=actionUrl;
    document.getElementById(formId).submit();
}

HTML ソース スニペット:

<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 が呼び出されます。

私はもう試した:

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 を使用してみました私も試しました:

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();

これらは両方とも、このボードや他のボードの例から来ていますが、添付ファイルの代わりに元のページを取得し続けています. また、必要なリターンウィンドウ/ドキュメントが以前のものであるため、適切なページ応答の履歴を確認する必要があるかどうかも疑問に思っています。完全な説明または良い例のドキュメントへの丁寧なリンクと、私が試すことができるソースは高く評価されます。

4

1 に答える 1

1

実際の例を見たことがないので、これは他の人に役立つかもしれないと思います。

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内のリンクをクリックするのに苦労しています

于 2013-07-15T17:05:05.257 に答える