2011-09-27 18 views
13

HtmlUnit'te formun fireEvent şeklinde gönderilebildiğini ve gönderileceğini biliyorum. Ama ya javascript'i devre dışı bırakmış ve bazı yerleşik işlevler kullanarak bir form göndermek istiyorsam?HtmlUnit, gönder düğmesine tıklamadan nasıl form gönderilir?

javadoc'u kontrol ettim ve bunu yapmanın herhangi bir yolunu bulamadım.


Ben HtmlUnit'in sayfasındaki javadoc ve öğretici okumak ve ben getInputByName() kullanmak ve bunu tıklayabilirsiniz Bil ki ... HtmlForm böyle bir işlevi olduğunu garip. BuT bazen, no'lu gönderim türü olmayan formlar veya isim düğmesi olmaksızın böyle bir düğme bile vardır.

Böyle bir durumda yardım isterim, bu yüzden fireEvent kullanıyorum ama her zaman işe yaramıyor. 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(); 
} 
+0

kullanıyorum tavsiye ederim bir 'HttpURLConnection' ve talimatlar özetlenen takip [burada] (http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests). Veya Apache'nin "HttpClient" sınıfını kullanın. – mrkhrts

+0

JavaDoc'u tekrar kontrol edin :) Veya ayrıca Ransom Briggs'in yaptığı gibi Giriş -> Başlarken bölümü. Ben mrkhrts yaklaşımı için gitmek olmaz ... çok düşük seviye –

cevap

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

Bir 'geçici' Gönder düğmesini kullanabilirsiniz:

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

OP düzenlenmiş ve şimdi bir gönderme düğmesi olmadığını söylüyor. – Gray

38

itibaren

+1

Bu muhteşem – Leo

+0

Sen benim arkadaşım bir dahiya! Bir hafta boyunca bu işte çalışıyorum! – duffanpj

+0

Bu çözüm, geçmişte çok fazla çini kullandım ve hiç problem yaşamadım. –

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

Nasıl kullanımını alma hakkında yerleşik javascript desteği? Sadece bu formdaki olayı gönder:

Kod, sitede ilk formu göndermek istediğinizi varsayar.

Ve başka bir siteye ileriye sen göndermeniz durumunda, sadece sayfa değişkene yanıtı bağlantı:

HtmlForm form = page.getForms().get(0); 
page = (HtmlPage) form.fireEvent(Event.TYPE_SUBMIT).getNewPage(); 
İlgili konular