Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an nonexistent field in Python ClientForm?

I'm using mechanize (which uses clientform) for some web crawling in python and since it doesn't support JS, I want to set a value of an unexistent input in a form (the input is generated by JS). How can I do this?

The error is similar to the one you get if you try to execute

from mechanize import Browser
br = Browser()
page = br.open('http://google.com')
br.select_form(nr = 0)
br['unexistent'] = 'hello'
like image 887
Fluffy Avatar asked Oct 10 '09 20:10

Fluffy


1 Answers

You need to first add the control to the form, and then fixup the form.

br.form.new_control('text','unexistent',{'value':''})
br.form.fixup()
br['unexistent'] = 'hello'

This really isn't very well documented, and in the source under fixup() there is the comment:

This method should only be called once, after all controls have been
added to the form.

However, it doesn't look like it does anything too dangerous. Probably at least add the control first before messing with anything else in the form.

like image 168
ABentSpoon Avatar answered Nov 11 '22 11:11

ABentSpoon