Python Mechanize : how to select a dropdown list when two have the same name in web page?

标签 python mechanize

我试图让 Mechanize 解析的 html 是:

<select id="topic_id2" name="topics[]" title="blabla" tabindex="4" class="createSelect">
here go options

但是在它的正下方有另一个下拉菜单,代码如下:

<select id="topic_id3" name="topics[]" title="optional" tabindex="5" class="createSelect">

现在如果它有帮助的话,我不需要从后一个中选择任何值,因为它是可选的。

当我尝试

br = mechanize.Browser()
br.select_form(name="form")
br["topics[]"] = ["Internet"]

我得到:

mechanize._form.AmbiguityError: more than one control matching name 'topics[]'

有没有一种方法可以使用 mechanize.Browser()(同时保留所有其他表单语法)根据其 ID 选择控件?

谢谢

最佳答案

mechanize 的外部文档非常小,仅包含几个示例,但代码内文档要广泛得多。

没有对此进行测试,使用名为 formHTMLForm 实例,您应该能够调用 form.find_control(id="topic_id3") 得到你想要的。我不确定如何仅使用 Browser 对象来执行此操作,但您是否尝试过 br.find_control(id="topic_id3")

关于Python Mechanize : how to select a dropdown list when two have the same name in web page?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7459488/

相关文章:

ruby - 如何使用 Mechanize 从 html 页面上的 'td' 标签中获取 'table' 标签中的文本

python - 使用 python mechanize 填充 <noscripts> 标签内的 textarea

python - 如何在 Python 中向量化这些嵌套循环?

python - 如何使用mechanize作为当前真正的firefox浏览器?

python - 这两种创建多维列表的方式的奇怪行为

javascript - 正则表达式 - 如果在大括号内则不匹配

python - 为什么我的 python mechanize 脚本无法用于该特定站点

ruby - 使用 Mechanize 的 Tumblr 登录脚本

python - 在原生Python中解析DICOM文件

python - 更改日期对象 Python 的时区