从下拉列表中机械化 select

Mechanize select from dropdown

我想 mechanize 检查所选下拉列表的当前值是否 = 默认值,然后 mechanize 将选择列表中的另一个值。 下拉的html如下:

            <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---

我的代码是:

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

但是列表值仍然 returns:

   ['---']

有什么想法吗?

您需要将值指定为列表:

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

根据 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