vendredi 4 décembre 2015

Django WebTest: Check/uncheck checkbox by value

I've got a form with a set of checkboxes (all under the same field name) in my WebTest response, and I'd like to uncheck some of them according to their value. I've tried this:

my_form = response.forms['form-i-want']
for i in range(len(my_form.fields.get('field-i-want'))):
    if my_form.fields.get('field-i-want')[i].value == "value-to-uncheck":
        my_form.fields.get('field-i-want')[i].checked = False

Obviously this is very hacky looking code and there must be a better way. Also, this doesn't actually uncheck the box I want: when I then iterate through the checkboxes in the form there is no longer an element with the value value-i-want: the value has been set to None. And when I submit the form it behaves as if the nothing was done to the form.




Aucun commentaire:

Enregistrer un commentaire