Mechanize select from dropdown

旧街凉风 提交于 2019-12-13 02:04:51

问题


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:


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


来源:https://stackoverflow.com/questions/30934003/mechanize-select-from-dropdown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!