Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an item for dropdown menu with mechanize in python?

I am REALLY confused. I'm basically trying to fill out a form on a website with mechanize for python. I got everything to work except the dropdown menu. What do I use to select it and what do I put for the value? I don't know if I'm supposed to put the name of the selection or the numerical value of it. Help would be greatly appreciated, thanks.

Code snippet:

try:
        br.open("http://www.website.com/")
        try:
            br.select_form(nr=0)
            br['number'] = "mynumber"
            br['from'] = "[email protected]"
            br['subject'] = "Yellow"
            br['carrier'] = "203"
            br['message'] = "Hello, World!"
            response = br.submit()
        except:
            pass
    except:
        print "Couldn't connect!"
        quit

I'm having trouble with the carrier, which is a dropdown menu.

like image 969
user962889 Avatar asked Sep 24 '11 18:09

user962889


1 Answers

According to the mechanize documentation examples, you need to access attributes of the form object, not the browser object. Also, for the select control, you need to set the value to a list:

br.open("http://www.website.com/")
br.select_form(nr=0)
form = br.form
form['number'] = "mynumber"
form['from'] = "[email protected]"
form['subject'] = "Yellow"
form['carrier'] = ["203"]
form['message'] = "Hello, World!"
response = br.submit()
like image 143
infrared Avatar answered Sep 18 '22 08:09

infrared