2016-10-12 5 views
1

HtmlUnitを使用してフォーム内にないオプションを選択しますか?それから私は結果として得られるページを取得する必要があります。ここに私が試したものがあります:フォーム内にないHtmlUnit選択オプション

public String getNewPage() throws Exception { 
    try (final WebClient webClient = new WebClient()) { 

     webClient.getOptions().setThrowExceptionOnScriptError(false); 
     webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); 
     webClient.getOptions().setPopupBlockerEnabled(true); 
     webClient.getOptions().setJavaScriptEnabled(true); 

     final HtmlPage page = webClient.getPage(URL); 

     HtmlOption option1 = (HtmlOption) page.getElementById("1"); 
     option1.removeAttribute("selected"); 
     HtmlOption option5 = (HtmlOption) page.getElementById("5"); 
     option5.setSelected(true); 

     // Some code missing here........ 

     return newHtmlString; 
} 

オプションがクリックされると、ページが自動的に更新されます。正しいオプションを選択した後、新しいページを取得するにはどうすればよいですか?私が何をしたか

+0

あなたはURLを提供できますか? –

+0

ありがとうございますが、私は問題の解決策を見つけました。下に投稿します。 – Yster

答えて

1

は、ほぼ正しかったが、何行方不明になったことは、次ました:

page.refresh(); 
    return page.asXml(); 

それから私がチェックとしてのチェックボックスをマークする別の問題がありました。ここに私のために働いた:

HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) page.getElementById("cb4"); 
    checkbox.setAttribute("checked", "checked"); 
    checkbox.fireEvent(Event.TYPE_CHANGE); 
    page.refresh(); 
    System.out.println(page.asXml()); 
関連する問題