Mechanize select from dropdown

3.7k views Asked by At

I want to mechanize to check if the current value of selected dropdown = the default value, then mechanize will choose another value in the list instead. The html of the dropdown is as follow:

            <td class="label">List</td>
            <td>
                <select name="list" id="list" onchange="list()">
                    <option>---</option>
                 <option value='1'>1</option>
<option value='2'>2</option>
---other options---

My code is:

if br.form["list"] == "---":
    br.form["list"].value = "1"
    r = br.form["list"]
    print(r)

However list value still returns:

   ['---']

Any idea?

1

There are 1 answers

0
falsetru On BEST ANSWER

You need to specify the value as a list:

if br.form["list"] == ["---"]:
    br.form["list"].value = ["1"]

According to the mechanize - Forms documentation:

# Controls that represent lists (checkbox, select and radio lists) are
# ListControl instances.  Their values are sequences of list item names.
# They come in two flavours: single- and multiple-selection:
form["favorite_cheese"] = ["brie"]  # single
form["cheeses"] = ["parmesan", "leicester", "cheddar"]  # multi