Hello I am trying to fill in a form using WWW::Mechanize. I can fill in 2 of the 3 fields but one is giving me a problem. The name of this field is "name" when I use the following bit of code, it seems to change the name of the form, not the value of the field. The part of original page after pretty printing {forms #<WWW::Mechanize::Form {name "wp_pers_form"} {method "GET"} {action "http://www.whitepages.com/10866/search/FindPerson"} {fields #<WWW::Mechanize::Field:0x2b729834b4b8 @name="firstname", @value=""> #<WWW::Mechanize::Field:0x2b7298347e08 @name="name", @value=""> The following bit of code seems to rename the form: page_form = page.form(''wp_pers_form'') page_form.name = ''smith'' page_form.firstname = ''joe'' Here is the first bit of the page form after pretty printing #<WWW::Mechanize::Form {name "smith"} {method "GET"} {action "http://www.whitepages.com/10866/search/FindPerson"} {fields #<WWW::Mechanize::Field:0x2b7299d0d8d8 @name="firstname", @value="joe"> #<WWW::Mechanize::Field:0x2b7299d0b6a0 @name="name", @value=""> #<WWW::Mechanize::Field:0x2b7299d0a778 @name="where", @value="miami,fl">} Any suggestions to fix this? Is this a bug? thanks scott
Hey scott! On Sat, Jul 28, 2007 at 03:26:53PM -0400, scott hankins wrote:> Hello > > I am trying to fill in a form using WWW::Mechanize. I can fill in 2 of > the 3 fields but one is giving me a problem. The name of this field is > "name" when I use the following bit of code, it seems to change the > name of the form, not the value of the field. > > The part of original page after pretty printing > > {forms > #<WWW::Mechanize::Form > {name "wp_pers_form"} > {method "GET"} > {action "http://www.whitepages.com/10866/search/FindPerson"} > {fields > #<WWW::Mechanize::Field:0x2b729834b4b8 @name="firstname", @value=""> > #<WWW::Mechanize::Field:0x2b7298347e08 @name="name", @value=""> > > The following bit of code seems to rename the form: > > page_form = page.form(''wp_pers_form'') > page_form.name = ''smith'' > page_form.firstname = ''joe'' > > Here is the first bit of the page form after pretty printing > > #<WWW::Mechanize::Form > {name "smith"} > {method "GET"} > {action "http://www.whitepages.com/10866/search/FindPerson"} > {fields > #<WWW::Mechanize::Field:0x2b7299d0d8d8 @name="firstname", @value="joe"> > #<WWW::Mechanize::Field:0x2b7299d0b6a0 @name="name", @value=""> > #<WWW::Mechanize::Field:0x2b7299d0a778 @name="where", @value="miami,fl">} > > Any suggestions to fix this? Is this a bug?I don''t know if I would call this a bug, but it is a little known "feature". The problem is that forms can have a name, and they can also contain fields named "name". Since I made form input fields act like accessors on the form object, it is possible for one to clobber the other. Which is exactly what you are running into. What I suggest is that you treat the form object like a hash. That will guarantee that you set those fields on the form. For example: page_form = page.form(''wp_pers_form'') page_form[''name''] = ''smith'' page_form[''firstname''] = ''joe'' Hope that helps! -- Aaron Patterson http://tenderlovemaking.com/
On 7/28/07, Aaron Patterson <aaron at tenderlovemaking.com> wrote:> I don''t know if I would call this a bug, but it is a little known > "feature". The problem is that forms can have a name, and they can also > contain fields named "name". Since I made form input fields act like > accessors on the form object, it is possible for one to clobber the > other. Which is exactly what you are running into. > > What I suggest is that you treat the form object like a hash. That will > guarantee that you set those fields on the form. For example: > > page_form = page.form(''wp_pers_form'') > page_form[''name''] = ''smith'' > page_form[''firstname''] = ''joe'' > > Hope that helps!Thanks for your help. That worked perfectly. I am pretty new at Ruby (and programming in general), is there a reason this would be considered a ''feature''? Why would you want to change the name of the form? thanks scott
On Sat, Jul 28, 2007 at 06:10:59PM -0400, scott hankins wrote:> On 7/28/07, Aaron Patterson <aaron at tenderlovemaking.com> wrote: > > > I don''t know if I would call this a bug, but it is a little known > > "feature". The problem is that forms can have a name, and they can also > > contain fields named "name". Since I made form input fields act like > > accessors on the form object, it is possible for one to clobber the > > other. Which is exactly what you are running into. > > > > What I suggest is that you treat the form object like a hash. That will > > guarantee that you set those fields on the form. For example: > > > > page_form = page.form(''wp_pers_form'') > > page_form[''name''] = ''smith'' > > page_form[''firstname''] = ''joe'' > > > > Hope that helps! > > Thanks for your help. That worked perfectly. > > I am pretty new at Ruby (and programming in general), is there a > reason this would be considered a ''feature''? Why would you want to > change the name of the form?I can''t think of a case where I would want to change the form name. :-) The reason I did that was to make behavior consistent. The setter and getter should access the same variable. In the case of "name", the meaning is ambiguous. When do you want the form name vs the "name" field of a form? -- Aaron Patterson http://tenderlovemaking.com/