Search code examples
javahtmlunit

HtmlUnit ClassCastException while selecting form button


I have a form - https://racky.wufoo.com/forms/m1wv5fg40e46lwk/

I am trying to use HtmlUnit to fill the form.

This is my code -

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("https://racky.wufoo.com/forms/m1wv5fg40e46lwk/");
HtmlSubmitInput button = page.getElementByName("saveForm");
HtmlTextInput field1 = page.getElementByName("Field1");
HtmlTextInput field2 = page.getElementByName("Field4");
field1.setValueAttribute("test_test");
field2.setValueAttribute("packpack");
HtmlPage page2 = button.click();

But when I am executing it I get the following error at HtmlSubmitInput button line -

java.lang.ClassCastException:com.gargoylesoftware.htmlunit.html.HtmlImageInput cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlSubmitInput

What should I do to resolve it?


Solution

  • page.getElementByName("saveForm");
    

    Your problem is because this return a HtmlImageInput in your case. Change your code like this:

    HtmlInput button = page.getElementByName("saveForm");