[twill] Getting All ID's or Names from a Form

Titus Brown titus at caltech.edu
Sun Jun 4 21:05:42 PDT 2006


Hi, terry,

sorry for taking so long to get to this.  I've been busy...

On Tue, May 09, 2006 at 07:50:31AM -0500, Terry Peppers wrote:
-> 
-> I'm having a similar problem. Let's say I have a form that is like the
-> following:
-> 
-> <form id="frm" name="foo" action="http://foo.com/do_something.php"
-> method="POST" enctype="application/x-www-form-urlencoded">
-> <input id="1001" name="select_1001" type="submit" value="Get It!" />
-> <input id="1001" name="select_1001" type="submit" value="Get It!" />
-> <input id="1002" name="select_1001" type="submit" value="Get It!" />
-> <input id="1002" name="select_1001" type="submit" value="Get It!" />
-> </form>
-> 
-> OK, so why are there two buttons on the page that have the same value?
-> I can't answer that, all I can say is that this is the hand I've been
-> dealt and I'm not sure how to get around it. So anyway, after looking
-> @ the Brett/Titus thread on how to pull out values, I figured I could
-> use the same concept to pull out 'id' and 'name.' Here's my stab @ it
-> using the Python interpretter and Twill.

[ munch ]

-> >>> field = b.get_form_field(form, "select_1001")
-> Traceback (most recent call last):
->   File "<stdin>", line 1, in ?
->   File "/usr/lib/python2.4/site-packages/twill-0.8.4-py2.4.egg/twill/browser.py",
-> line 375, in get_form_field
->     raise Exception('multiple matches to "%s"' % (fieldname,))
-> Exception: multiple matches to "select_1001"
-> <end code>
-> 
-> So that's my question, if I have two sets of identical buttons how can
-> I access either the 'name' values or the 'id' values? In a perfect
-> world I could put the contents in a list and randomly pick whatever
-> comes back. I was trying to do this with a regular expression, but I
-> think that would be pretty djanky and not suck a good idea.

Well... what do you want to do with them? ;)  Do you want to submit
with them, or set the value, or ...?  All of the HTML parsing is pretty
simple with BeautifulSoup, but if you want to do things to the results with
twill or mechanize it gets a bit tougher.

You can use an explicit number with 'fv' e.g. 'fv formid 5' to get the
5th thing that showforms shows you, too.

cheers,
--titus



More information about the twill mailing list