java – HtmlUnit – Get radio by value-ThrowExceptions

Exception or error:

How can I, using HtmlUnit, get the specific element?

My target page contains 9 radios all like this:

<input type="radio" name="totalpoll[choices][]" value="9" class="totalpoll-choice-checkbox">

The only differentiating part is the value.

I’m learning HtmlUnit so any suggestions are welcome!

How to solve:

You can do something like this

for (DomElement elem : page.getElementsByTagName("input")) {
    if (elem instanceof HtmlRadioButtonInput
            && "<your text>".equals(((HtmlRadioButtonInput) elem).getValueAttribute())) {

        // elem is the radio button your a looking for
        break;
    }
}

###

have different id’s for each element, or use different name in the name attribute.

Leave a Reply

Your email address will not be published. Required fields are marked *