Python使用下拉提交机械化复选框

时间:2013-04-08 13:24:25

标签: python drop-down-menu submit mechanize mechanize-python

我有一个电子邮件客户端。我使用python mechanize检查我需要做的事情的电子邮件的“复选框”(move,detele,egg):

br.open(login_url)
br.open(url_1)
br.select_form(nr=0)
for i in range(0, len(br.find_control(type="checkbox").items)):
    if "4" in str(br.find_control(type="checkbox").items[i]):
        br.find_control(type="checkbox").items[i].selected = False
    else:
        br.find_control(type="checkbox").items[i].selected = True
reponseaa = br.submit()
print reponseaa

br.open(url_2)
br.select_form(nr=2)
for i in range(0, len(br.find_control(type="checkbox").items)):
    if "45198" and "4519" in str(br.find_control(type="checkbox").items[i]):
        br.find_control(type="checkbox").items[i].selected = True
    else:
        br.find_control(type="checkbox").items[i].selected = False
br.form['self'] = ["88"]
r1 = br.submit()
print r1

问题在于提交按钮,这是一个下拉菜单,如下所示:

<select name="self">
<option value="0">Select an action</option>
<option value="87">Move</option>
<option value="88">Delete</option>
<option value="89">Mark as Important</option>
</select>
<input type="submit" name="submit" value="Action!">

如何选择删除并按下提交按钮?代码示例会很棒。感谢。

1 个答案:

答案 0 :(得分:3)

您应该使用form from mechanize

import mechanize

br = mechanize.Browser()
br.open(URL)
br.select_form(nr=0)
br.form['self'] = ["88"]
r = br.submit()

print r.read()