2011-09-27 21 views
13

Sé que en HtmlUnit puedo fireEvent enviar en el formulario y será publicado. Pero, ¿qué ocurre si desactivo javascript y me gustaría publicar un formulario utilizando alguna función incorporada?HtmlUnit, ¿cómo publicar el formulario sin hacer clic en el botón Enviar?

He comprobado el javadoc y no he encontrado ninguna manera de hacerlo. Es extraño que no existe tal función en HtmlForm ...


leí el javadoc y un tutorial en la página HtmlUnit y sé que puedo usar getInputByName() y haga clic en él. Pero a veces hay formularios que no tienen botón de enviar tipo o incluso hay tal botón pero sin nombre de atributo.

Estoy pidiendo ayuda en tal situación, es por eso que estoy usando fireEvent pero no siempre funciona.

+0

le recomiendo usar un 'HttpURLConnection' y siga las instrucciones descritas [aquí] (http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests). O usa la clase 'HttpClient' de Apache. – mrkhrts

+0

Comprueba JavaDoc nuevamente :) O también en la sección Introducción -> Introducción, como lo sugiere Ransom Briggs. No me gustaría el enfoque de mrkhrts ... es un nivel muy bajo –

Respuesta

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

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

El OP fue editado y ahora dice que no hay un botón de envío. – Gray

38

puede usar el botón de un 'temporal' submit:

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

esto es brillante – Leo

+0

¡Mi amigo es un genio! ¡He estado trabajando alrededor de esto por una semana! – duffanpj

+0

Es la solución, la usé mucho en el pasado y nunca tuve ningún problema. –

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

¿Qué le parece el uso de una función de soporte javascript? Simplemente ejecute el evento enviar en ese formulario:

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

El código supone que desea enviar el primer formulario en el sitio.

Y si el le hacia delante someterse a otro sitio, simplemente enlazar la respuesta a la variable de página:

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