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

那年仲夏 提交于 2019-12-07 06:21:25

问题


The html that I'm trying to make mechanize parse is:

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

But then right below it there is another dropdown, with the following code:

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

Now if it helps at all, I need not select any value from the latter one, since it is optional.

When I try

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

I get:

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

Is there a way I can select a control based on its id, using mechanize.Browser() (while retaining all the other form syntax)?

Thanks


回答1:


The external documentation for mechanize is quite small and contains just a few examples, but the in-code documentation is far more extensive.

Not having tested this, with an HTMLForm instance called form you should be able to call form.find_control(id="topic_id3") and get what you want. I'm not sure how to do this with just a Browser object, but have you tried br.find_control(id="topic_id3")?



来源:https://stackoverflow.com/questions/7459488/python-mechanize-how-to-select-a-dropdown-list-when-two-have-the-same-name-in-w

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