2

I'm writing some web tests with the Django webtest where I'm trying to set one radio button in a pair of radio buttons to be checked. Naturally, they both have the same name attribute value.

Using pdb, and assuming I have a form variable whose type is webtest.forms.Form, here's what I see if I type form.html, i.e. what Webtest has been handed by beautifulsoup (only part of it, but the relevant part).

<input name="is_external" type="radio" value="False"/>
<input name="is_external" type="radio" value="True"/>

Normally to select a specific input element which shares a name with other input elements I'd do something like form.get('is_external', index=1) which would in this example select the input whose value is True.

However, when I do this I'm merely given the error message:

*** IndexError: list index out of range.

form.get('is_external') on its own will only give me the first one. I've checked the docs and they are sadly lacking when it comes to radio buttons.

Am I missing something? I've used this method with type="checkbox" elements before and I've had no problems.

basicallydan
  • 2,134
  • 3
  • 25
  • 40

1 Answers1

1

A colleague gave me some information that led to figuring out an answer.

It turns out that according to Webtest, Radio buttons are seen as a single item in the Form API. So in order to select another one, you need to use:

form['is_external'].select('True')

That way, the second input in my example is given checked="checked". So it's not possible to select a specific radio button in the Webtest Form class.

basicallydan
  • 2,134
  • 3
  • 25
  • 40
  • This is generally the case with radio buttons with the same name - particularly as users can only select a single radio button at a time. Better to think of them as being a different way of displaying a select. – John Montgomery Feb 18 '16 at 16:42
  • Thanks John! I can see it now, it does make sense but I guess I thought it'd have a similar API to a checkbox and webtest would just unselect any radio button that had the same name. Cheers for the input :) – basicallydan Feb 19 '16 at 07:28