Skip to content Skip to sidebar Skip to footer

Differentiating Between Html Form SELECT Items With The Same Name

I'm trying to dynamically fill a form in Python using Mechanize. However, when I inspected the source of the HTML page that has the form, I realized that some of the controls on th

Solution 1:

You can use BeautifulSoup to parse the response to get the select options

import mechanize
from BeautifulSoup import BeautifulSoup
br = mechanize.Browser()
resp = br.open('your_url')
soup = BeautifulSoup(resp.get_data())
second_select = soup.findAll('select', name="mv_searchspec")[1]
# now use BeautifulSoup API to get the data you want

You can't do something like br['mv_searchspec'] = 'foo' as obviously this is ambigious. You should be able to do this though

br.select_form(nr=0) # select index
controls = br.form.controls
controls[desired_index]._value = 'your_value'
...
br.submit()

Solution 2:

As zneak said, the handling is backend specific. The important thing to realize is that each element with the attribute 'name' specified will be sent via the POST/GET, even if it is empty. Thus the way to differentiate between them is simply by their order.


Post a Comment for "Differentiating Between Html Form SELECT Items With The Same Name"