0

ページから約 300 個のリンクを取得し、それらをショートカット (すべてフォルダーに保存) にする小さなスクリプトを作成しています。

一部のページから必要なすべてのリンクを取得できますが、最初にログインする必要がある Web サイトの一部です。

HttpUnit を試しましたが、毎回失敗します。今までは、Html ページを inputStream に入れて、そこから (1 行ずつ、必要なものを探して) 読み取るだけでしたが、ログイン部分に到達したら、Web サイトに接続する方法や他のことを行う方法がわかりません。

誰にでも役立つ場合は、HttpUnitコードを次に示します。

final WebClient webClient = new WebClient();

// Get the first page
final HtmlPage page1 = webClient.getPage("mywebsite");

ArrayList<HtmlForm> f;
f = (ArrayList<HtmlForm>) page1.getForms();

System.out.println(f);

// Get the form that we are dealing with and within that form, 
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getFirstByXPath("//form[@id='login']");

final HtmlSubmitInput button = form.getFirstByXPath("//input[@value='Login']");
final HtmlTextInput username = form.getFirstByXPath("//input[@id='username']");

// Change the value of the text field
username.setValueAttribute("username");

final HtmlPasswordInput passField = form.getFirstByXPath("//input[@id='password']");

// Change the value of the text field
passField.setValueAttribute("pass");

// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();

webClient.closeAllWindows();

私の悪い変数名付けを許してください :p これは私のためだけのスクリプトなので、あまり気にしませんでした。

「final HtmlPage page2 = button.click();」で NullPointerException が発生します。

前もって感謝します。

4

1 に答える 1

0

ボタンの検索に失敗したようです。この行の後

final HtmlSubmitInput button = form.getFirstByXPath("//submit[@value='Login']");

追加します

assert(button != null) : "Could not find the button";

(JVM へのパラメーター) でアサーションを使用してアプリを実行する-eaと、その場でアサーションの失敗が報告されます。

于 2013-03-10T05:22:37.873 に答える