Search code examples
javahtmlunit

HtmlUnit get submit button from form by it's value?


<input type="submit" class="submit-button" style="white-space:normal" value="hello world!">

This is how the HTML submit button works, in HTMLUNIT I already got the form, using the list:

HtmlForm form = this.page.getForms().get(0);

But now the site im trying to load doesn't have id or name on the submit button, just a class & a value.

I tried doing this:

HtmlButton button = (HtmlButton) page.getByXPath("/html/body//form//input[@type='submit' and @value='Hello world!']");

But when it loads, I get this error:

Exception in thread "Thread-1" java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlButton
    at Web.getButton(Web.java:78)
    at java.lang.Thread.run(Unknown Source)

How can I get button without id or value?


Solution

  • Just use the getInputByValue(String) method on the form:

    HtmlForm form = this.page.getForms().get(0);
    System.out.println(form.getInputByValue("Hello world!").asXml());