Hello everyone!<br><br>First of all, I'd like to thank everyone involved with the twill project for creating and maintaining a very useful tool!<br><br>To time-deprived readers, I'll explain it briefly:<br><br>My problem is that unnamed forms that sit on a page that have a named form that has the unnamed form's number as part of its name cannot be accessed through regular fv's. For instance, if form #1 is named "Form-123" then unnamed forms #2 and #3 cannot be accessed by fv (2, , ) or fv ( "3", , ) because the numbers 2 and 3 and the strings "2" and "3" are matched to the "Form-123" form.
<br><br>I suppose that a pretty solution would be to subclass the twill browser and add a matching process that prioritizes number matching. But I cannot do so right now. <br> <br>So I made an ugly workaround: I set the unnamed form to a certain name and use it normally onwards, through the set name.
<br><br>To those who need more info, I'll explain in length:<br><br>We at <a href="http://stoa.usp.br/">http://stoa.usp.br/</a> are beginning to use twill to monitor our site for any "system breaks" that might follow one of the developers submitting poor code.
<br><br>Recently, we started using a Google-provided form that makes searches in our site. With this, our main page has the following forms:<br><br>>>> showforms()<br><br>Form name=searchform (#1)<br>## ## __Name__________________ __Type___ __ID________ __Value__________________
<br>1 tag text (None) <br>2 user_type select (None) [''] of ['', 'person', 'community', ... <br>3 1 None submit (None) Ok
<br><br><br>Form name=searchbox_006456382451512069951:41lst14sjem (#2)<br>## ## __Name__________________ __Type___ __ID________ __Value__________________<br>1 cx hidden (None) 006456382451512069951:41lst14sjem
<br>2 cof hidden (None) FORID:0 <br>3 q text (None) <br>4 1 sa submit (None) Search <br><br><br>Form #3<br>## ## __Name__________________ __Type___ __ID________ __Value__________________
<br>1 username text username <br>2 password password password <br>3 passthru_url hidden (None) <a href="http://stoa.usp.br/">http://stoa.usp.br/
</a> <br>4 action hidden (None) log_on <br>5 1 submit submit (None) Ok <br>6 remember checkbox (None) ['on'] of ['on']
<br><br>As you can see, form #2 is the Google-provided form, with a long string as its name, and this string contains numbers, note specially that it contains the numbers 1 and 3.<br><br>Now, I need to fv the unnamed form #3, but if I try it directly I get an error:
<br><br>>>> fv (3, "username", "test")<br>Traceback (most recent call last):<br> File "<stdin>", line 1, in ?<br> File "/usr/lib/python2.4/site-packages/twill-0.9b1-py2.4.egg
/twill/commands.py", line 428, in formvalue<br> control = browser.get_form_field(form, fieldname)<br> File "/usr/lib/python2.4/site-packages/twill-0.9b1-py2.4.egg/twill/browser.py", line 366, in get_form_field
<br> raise Exception('no field matches "%s"' % (fieldname,))<br>Exception: no field matches "username"<br>>>> fv ("3", "username", "test")<br>Traceback (most recent call last):
<br> File "<stdin>", line 1, in ?<br> File "/usr/lib/python2.4/site-packages/twill-0.9b1-py2.4.egg/twill/commands.py", line 428, in formvalue<br> control = browser.get_form_field(form, fieldname)
<br> File "/usr/lib/python2.4/site-packages/twill-0.9b1-py2.4.egg/twill/browser.py", line 366, in get_form_field<br> raise Exception('no field matches "%s"' % (fieldname,))<br>Exception: no field matches "username"
<br><br>The reason is that twill matches 3 or "3" first to the Google-provided form, because it has the number 3 as part of it's string name! I get the "no field matches" error because there is no "username" at the Google-provided form!
<br><br>Now I hope there is a better way to solve this issue (other than rewriting the site), but I came up with this ugly solution:<br><br>I retrieve a forms list with a call to a browser object's get_all_forms() method;
<br>I name the third form as "login";<br>I use the string "login" to use such form, instead of using the number 3.<br><br>It works, check it out:<br><br>>>> b = get_browser()<br>>>> b.get_all_forms
()[2].name = "login"<br>>>> fv ("login", "username", "test")<br>>>> showforms()<br><br>Form name=searchform (#1)<br>## ## __Name__________________ __Type___ __ID________ __Value__________________
<br>1 tag text (None) <br>2 user_type select (None) [''] of ['', 'person', 'community', ... <br>3 1 None submit (None) Ok
<br><br><br>Form name=searchbox_006456382451512069951:41lst14sjem (#2)<br>## ## __Name__________________ __Type___ __ID________ __Value__________________<br>1 cx hidden (None) 006456382451512069951:41lst14sjem
<br>2 cof hidden (None) FORID:0 <br>3 q text (None) <br>4 1 sa submit (None) Search <br><br><br>Form name=login (#3)
<br>## ## __Name__________________ __Type___ __ID________ __Value__________________<br>1 username text username test <br>2 password password password <br>3 passthru_url hidden (None)
<a href="http://stoa.usp.br/">http://stoa.usp.br/</a> <br>4 action hidden (None) log_on <br>5 1 submit submit (None) Ok <br>6 remember checkbox (None) ['on'] of ['on']
<br><br>Well, I know it ain't pretty, but it works. At least until the developers change the front page again. I thought this would be of interest to anyone having problems with named and unnamed forms on the same page: the problem is, possibly due to a form that has numbers as part of its name.
<br><br>Again, I'd like to thank Titus and the rest of the crew. Great work!<br><br>See ya!<br>Renato.<br><br>