目前正在使用Mechanize提交一些表格
这是我目前的代码片段:
add_control = br.form.find_control(name='CRN_IN', id='crn_id1')
总共有10个文本框,我使用以下代码打印:
for form in br.forms():
print "Form name:", form.name
print form
这是输出:
<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)>
如我的代码片段所示,我可以使用id='crn_id1'
选择1到10个文本框,一直到id=crn_id10
。但是,我的问题是,当我尝试以下内容时:
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()
我收到以下错误:
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__'
编辑:
将.select_form()
添加到find_control
行后,我收到以下错误:
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'
答案 0 :(得分:1)
只需添加:
add_control.value = '33587'