机械化下拉菜单中的选择 [英] Mechanize select from dropdown

查看:93
本文介绍了机械化下拉菜单中的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要机械化检查所选下拉列表的当前值是否为默认值,然后机械化将在列表中选择另一个值. 下拉菜单的html如下:

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

我的代码是:

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

但是列表值仍然返回:

   ['---']

有什么主意吗?

推荐答案

您需要将值指定为列表:

You need to specify the value as a list:

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

根据 mechanize-表单文档:

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

这篇关于机械化下拉菜单中的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆