2

I been trying to fix the code below for a while now, for some reason every part of the code works except the part where it has to click on a radio button, it says "ValueError: Option 1 not found in field " but there is an options that's for 1, it's either 0 or 1. I used to play with mechanize I know what I'm doing for the most part, but can anyone help? Please & Thank you

from robobrowser import RoboBrowser
browser = RoboBrowser(parser='html.parser')
browser.open(url)

form = browser.get_forms()[0] 
form['username'] = 'foo'
form['password'] = 'foo'
form['gender'] = [1] #Radio button

browser.session.headers['Referer'] = url
browser.submit_form(form)

2 Answers2

3

This is the same problem as this one: Setting a plain checkbox with robobrowser

This code should work for you:

form['gender'].options = ['1']
form['gender'].value = '1'
Community
  • 1
  • 1
burgund
  • 165
  • 1
  • 5
1

Sometimes RoboBrowser does not group radio buttons with the same name, no so you can find yourself with two separate "gender" fields.

Here is a solution to cope with that:

radios = form.fields.poplist('gender')
form.add_field(radios[0])
form['gender'].options += ['1']
form['gender'] = '1'
Jean Paul
  • 1,439
  • 18
  • 21