2011-09-27 16 views
13

私はHtmlUnitで私ができることを知っていますfireEvent投稿するとそれが掲載されます。しかし、どうすればjavascriptを無効にし、組み込みの関数を使ってフォームを投稿したいのですか?HtmlUnit、送信ボタンをクリックせずにフォームを投稿する方法は?

javadocを確認したところ、これを行う方法が見つかりませんでした。私がhtmlunitページ上のjavadocやチュートリアルを読んで、私はgetInputByName()を使用して、それをクリックすることができます知っている... HtmlFormにはそのような機能がないこと


奇妙です。場合によってはサブタイプボタン を持たないフォームや、そのようなボタンがありますが名前属性がないフォームがあります。

私はこのような状況で助けを求めています。これが私がfireEventを使用している理由ですが、必ずしもそうではありません。ビルトインのJavaScriptサポートの使用を得ることについて

WebClient client = new WebClient(); 
HtmlPage page = client.getPage("http://stackoverflow.com"); 

// create a submit button - it doesn't work with 'input' 
HtmlElement button = page.createElement("button"); 
button.setAttribute("type", "submit"); 

// append the button to the form 
HtmlElement form = ...; 
form.appendChild(button); 

// submit the form 
page = button.click(); 
+0

私は 'HttpURLConnection'を使用することをお勧めして説明した手順に従ってくださいしたい[ここ](のhttp:/ /stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests)。あるいは、Apacheの 'HttpClient'クラスを使用してください。 – mrkhrts

+0

JavaDocをもう一度チェックしてください:)また、Ransom Briggsのように、Introduction - > Getting Startedセクションも確認してください。私はmrkhrtsのアプローチに行くつもりはない...それはあまりにも低いレベルです –

答えて

2
final HtmlSubmitInput button = form.getInputByName("submitbutton"); 
final HtmlPage page2 = button.click() 

?そのフォームにイベントを提出するだけです:

HtmlForm form = page.getForms().get(0); 
form.fireEvent(Event.TYPE_SUBMIT); 

このコードでは、最初のフォームをサイトに提出したいとします。

そして、別のサイトに転送し、あなたを提出するだけでページ変数への応答をリンクする場合:

HtmlForm form = page.getForms().get(0); 
page = (HtmlPage) form.fireEvent(Event.TYPE_SUBMIT).getNewPage(); 
+2

OPが編集され、今は送信ボタンがないと言います。 – Gray

38

から

+1

これは素晴らしいです – Leo

+0

私の友人は天才です! 1週間これを回避しようとしています! – duffanpj

+0

それは解決策です、私は過去に多くのタイルを使っていましたが、決して問題はありませんでした。 –

7
WebRequest requestSettings = new WebRequest(new URL("http://localhost:8080/TestBox"), HttpMethod.POST); 

// Then we set the request parameters 
requestSettings.setRequestParameters(Collections.singletonList(new NameValuePair(InopticsNfcBoxPage.MESSAGE, Utils.marshalXml(inoptics, "UTF-8")))); 

// Finally, we can get the page 
HtmlPage page = webClient.getPage(requestSettings); 
1

方法:あなたは '一時的に' ボタンを提出使用することができますthe htmlunit doc

@Test 
public void submittingForm() throws Exception { 
    final WebClient webClient = new WebClient(); 

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

    // 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.getFormByName("myform"); 

    final HtmlSubmitInput button = form.getInputByName("submitbutton"); 
    final HtmlTextInput textField = form.getInputByName("userid"); 

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

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

    webClient.closeAllWindows(); 
} 
関連する問題