theGreenCabbage
theGreenCabbage

Reputation: 4845

No control matching name with Python Mechanize

Currently using Mechanize to submit some forms

This is my current snippet of code:

add_control = br.form.find_control(name='CRN_IN', id='crn_id1')

There are a total of 10 textboxes, which I print using the following code:

for form in br.forms():                                                   
    print "Form name:", form.name                                         
    print form

This is the output:

  <TextControl(CRN_IN=)>
  <HiddenControl(assoc_term_in=) (readonly)>
  <HiddenControl(start_date_in=) (readonly)>
  <HiddenControl(end_date_in=) (readonly)>
  <HiddenControl(RSTS_IN=WR) (readonly)>

  <TextControl(CRN_IN=)>
  <HiddenControl(assoc_term_in=) (readonly)>
  <HiddenControl(start_date_in=) (readonly)>
  <HiddenControl(end_date_in=) (readonly)>
  <HiddenControl(RSTS_IN=WR) (readonly)>

  <TextControl(CRN_IN=)>
  <HiddenControl(assoc_term_in=) (readonly)>
  <HiddenControl(start_date_in=) (readonly)>
  <HiddenControl(end_date_in=) (readonly)>
  <HiddenControl(RSTS_IN=WR) (readonly)>

  <TextControl(CRN_IN=)>
  <HiddenControl(assoc_term_in=) (readonly)>
  <HiddenControl(start_date_in=) (readonly)>
  <HiddenControl(end_date_in=) (readonly)>
  <HiddenControl(RSTS_IN=WR) (readonly)>

  <TextControl(CRN_IN=)>
  <HiddenControl(assoc_term_in=) (readonly)>
  <HiddenControl(start_date_in=) (readonly)>
  <HiddenControl(end_date_in=) (readonly)>
  <HiddenControl(RSTS_IN=WR) (readonly)>

  <TextControl(CRN_IN=)>
  <HiddenControl(assoc_term_in=) (readonly)>
  <HiddenControl(start_date_in=) (readonly)>
  <HiddenControl(end_date_in=) (readonly)>
  <HiddenControl(RSTS_IN=WR) (readonly)>

  <TextControl(CRN_IN=)>
  <HiddenControl(assoc_term_in=) (readonly)>
  <HiddenControl(start_date_in=) (readonly)>
  <HiddenControl(end_date_in=) (readonly)>
  <HiddenControl(RSTS_IN=WR) (readonly)>

  <TextControl(CRN_IN=)>
  <HiddenControl(assoc_term_in=) (readonly)>
  <HiddenControl(start_date_in=) (readonly)>
  <HiddenControl(end_date_in=) (readonly)>
  <HiddenControl(RSTS_IN=WR) (readonly)>

  <TextControl(CRN_IN=)>
  <HiddenControl(assoc_term_in=) (readonly)>
  <HiddenControl(start_date_in=) (readonly)>
  <HiddenControl(end_date_in=) (readonly)>
  <HiddenControl(RSTS_IN=WR) (readonly)>

As shown in my snippet of code, I can select the 1 - 10 textboxes using the id='crn_id1', all the way up to id=crn_id10. However, my issue is, when I try the following:

add_control = br.form.find_control(name='CRN_IN', id='crn_id1')
add_control['CRN_IN'] = '34688' # this indicates a CRN to submit
response = br.submit()

I get the following error:

Traceback (most recent call last):
  File "./add2.py", line 37, in <module>
    add_control['CRN_IN'] = '34688'
  File "/usr/local/lib/python2.7/site-packages/mechanize/_form.py", line 1212, in __getattr__
    (self.__class__.__name__, name))
AttributeError: TextControl instance has no attribute '__setitem__'

EDIT:

After I add .select_form() to the find_control line, I am getting the following error:

Traceback (most recent call last):
  File "./add2.py", line 35, in <module>
    add_control = br.form.find_control(name='CRN_IN', id='crn_id1').select_form()
  File "/usr/local/lib/python2.7/site-packages/mechanize/_form.py", line 1212, in __getattr__
    (self.__class__.__name__, name))
AttributeError: TextControl instance has no attribute 'select_form'

Upvotes: 1

Views: 1868

Answers (1)

theGreenCabbage
theGreenCabbage

Reputation: 4845

Simply add:

add_control.value = '33587'

Upvotes: 1

Related Questions