views:

289

answers:

1

Got my little mechanize code:

br.open('http://tumblr.com/customize'); 
print br.response().read()
print br.form['edit_tumblelog[cname]'] # there definitely is edit_tumblelog
                                       # and br.form['edit_tumblelog[enable_cname]'] works fine

Output:

...
<br/>
                                    <input type="text" class="text_field" style="width:275px; min-width:0px;
                                    margin:6px 0px; border:solid 1px #d2d2d2;
                                    "
                                    name="cname" id="cname"
                                    onchange="form_changed = true;"
                                     value="blog.yay.com"    
                                    />
...
Traceback (most recent call last):
  File "/tmp/temp_textmate.W6p5gh", line 51, in <module>
    print br.form['edit_tumblelog[cname]']
  File "/Library/Python/2.6/site-packages/ClientForm-0.2.10-py2.6.egg/ClientForm.py", line 2891, in __getitem__
  File "/Library/Python/2.6/site-packages/ClientForm-0.2.10-py2.6.egg/ClientForm.py", line 3222, in find_control
  File "/Library/Python/2.6/site-packages/ClientForm-0.2.10-py2.6.egg/ClientForm.py", line 3306, in _find_control
ClientForm.ControlNotFoundError: no control matching name 'edit_tumblelog[cname]'

What am I doing wrong?

+1  A: 

Found the problem. It's a bug in Mechanize HTML parser that somehow ignores next tag after an occurence of <br/> note that <br /> works fine. My solution was to manually replace those:

response = br.response()
response.set_data(response.get_data().replace("<br/>", "<br />")) #Python mechanize is broken, fixing it.
br.set_response(response)

Obviously a better solution is to re.sub() all tags without space before />.

Andrew Kolesnikov