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

Terry Peppers peppers at gmail.com
Tue May 9 05:50:31 PDT 2006


I was looking through the archives of the Twill mailing list and
noticed this thread:

http://lists.idyll.org/pipermail/twill/2006-February/000190.html

Brett asks about how he can get form values and Titus gives a good
response on how to nab them.

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.

<begin code>
>> from twill import get_browser
>> from twill.commands import *
>> url = "http://foo.com"
>> go(url)
==> at http://foo.com
>> b = get_browser()
>> f = b.get_form("foo")
>> field = b.get_form_field(f, "name")
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 373, in get_form_field
    raise Exception('no field matches "%s"' % (fieldname,))
Exception: no field matches "name"
>>> 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.

t.



More information about the twill mailing list