Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 893
Bo Milanovich Avatar asked Nov 13 '22 17:11

Bo Milanovich


1 Answers

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")?

like image 148
taleinat Avatar answered Dec 24 '22 18:12

taleinat