10

When I run the following script:

from mechanize import Browser
br = Browser()
br.open(url)
br.select_form(name="edit_form")
br['file'] = 'file.txt'
br.submit()

I get: ValueError: value attribute is readonly

And I still get the same error when I add:

br.form.set_all_readonly(False)

So, how can I use Python Mechanize to interact with a HTML form to upload a file?

Richard

1
  • this may be stupid, but does the file.txt exist in your script's working directory? Commented Aug 19, 2009 at 18:18

2 Answers 2

18

This is how to do it properly with Mechanize:

br.form.add_file(open(filename), 'text/plain', filename)
Sign up to request clarification or add additional context in comments.

Comments

2

twill is built on mechanize and makes scripting web forms a breeze. See python-www-macro.

>>> from twill import commands
>>> print commands.formfile.__doc__

>> formfile <form> <field> <filename> [ <content_type> ]

Upload a file via an "upload file" form field.

>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.