Search code examples
javascripthtmlweb-scrapinghtmlunit

HTMLUnit next page on image without name or id


I am new to HTMLUnit (read about it but just starting with a hands-on). I have a web page with a form whose "next" button is a simple input image without name nor id:

<input src="https://static.XXX.com/imagenes/siguiente.gif" alt="SIGUIENTE" type="image">

The form head is as follows:

<form action="/lugar/" method="PUT" onsubmit="return validaFormulario()" id="frmStep2" name="frmStep2">

And the validation JavaScript is:

var _localidadObligatoria = true;
var _skipValidation = false;

function validaFormulario() {
  if (_skipValidation == true)
    return true;
  if ((document.getElementById('p').value == "df") || (_localidadObligatoria && document.getElementById("l").value == '')) {
    alert('Rellene los campos necesarios marcados con asterisco.');
    return false;
  }

  return true;
}

I have tried different proposals as this one which showed promising:

HtmlUnit, how to post form without clicking submit button?

But when I click on the newly created fake button I only go back to the same page.

Can someone explain me the login behind correctly submitting this form based on HTMLUnit/HTM/Javascript perspective?

Thanks in advance,

Jose


Solution

  • I finally managed to make this work myself. I post it in case it is of any help to someone:

    HtmlImageInput button = (HtmlImageInput) page.getFirstByXPath("//input[@src='hhttps://static.XXX.com/imagenes/siguiente.gif']");
    
    // submit the form
    try {
        page = (HtmlPage) button.click();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    

    It just works!