# Mechanize: find form with 'id' rather than 'name'?

**URL:** <https://rubytalk.org/t/mechanize-find-form-with-id-rather-than-name/34960>\
**Category:** ruby-talk\
**Created:** [31 January 2007 11:18 UTC](https://rubytalk.org/t/mechanize-find-form-with-id-rather-than-name/34960 "2007-01-31T11:18:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Szinek3](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Peter\_Szinek3](https://rubytalk.org/u/Peter_Szinek3)\
**Post date:** [31 January 2007 11:18 UTC](https://rubytalk.org/t/mechanize-find-form-with-id-rather-than-name/34960/1 "2007-01-31T11:18:37Z")

</div>

This is what I am doing:

my\_form = [doc.forms.with.name](http://doc.forms.with.name)(formname).first

This is what I want:

my\_form = [doc.forms.with.id](http://doc.forms.with.id)(id\_string).first

but somehow Mechanize does not like it.

Maybe because id collides with [Object.id](http://Object.id)?

My problem is that the from does \*not\* have a 'name' attribute in the html - so apparently I can not use that to locate it - therefore I would like to use 'id' - or what else should I do?

Thanks,  
Peter

> **···**
>
> \_\_  
> [http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![Andy\_Lester](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@Andy\_Lester](https://rubytalk.org/u/Andy_Lester)\
**Post date:** [31 January 2007 13:44 UTC](https://rubytalk.org/t/mechanize-find-form-with-id-rather-than-name/34960/2 "2007-01-31T13:44:58Z")

</div>

I haven't dug into the Ruby Mech yet, but the Perl version lets you pick the Nth form on the page.

> **···**
>
> On Jan 31, 2007, at 5:18 AM, Peter Szinek wrote:
> 
> > My problem is that the from does \*not\* have a 'name' attribute in the html - so apparently I can not use that to locate it - therefore I would like to use 'id' - or what else should I do?
> 
> --  
> Andy Lester =\> andy@petdance.com =\> [www.petdance.com](http://www.petdance.com) =\> AIM:petdance

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [31 January 2007 15:35 UTC](https://rubytalk.org/t/mechanize-find-form-with-id-rather-than-name/34960/3 "2007-01-31T15:35:17Z")

</div>

> This is what I am doing:
> 
> my\_form = doc.forms.with.name(formname).first
> 
> This is what I want:
> 
> my\_form = doc.forms.with.id(id\_string).first
> 
> but somehow Mechanize does not like it.
> 
> Maybe because id collides with Object.id?
> 
> My problem is that the from does \*not\* have a 'name' attribute in the  
> html - so apparently I can not use that to locate it - therefore I would  
> like to use 'id' - or what else should I do?

Here's at least one way:  
if doc is your instance of WWW::Mechanize:  
my\_form = (doc.page/:form).find { |elem| elem['id'] == id\_string }

(Of course if you did doc = mech\_instance.page, then it's  
(doc/:form).find ...)

> **···**
>
> On Wed, Jan 31, 2007 at 08:18:37PM +0900, Peter Szinek wrote:
> 
> > Thanks,  
> > Peter
> > 
> > \_\_  
> > [http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![Mat\_Schaffer](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Mat\_Schaffer](https://rubytalk.org/u/Mat_Schaffer)\
**Post date:** [31 January 2007 13:54 UTC](https://rubytalk.org/t/mechanize-find-form-with-id-rather-than-name/34960/4 "2007-01-31T13:54:30Z")

</div>

Ruby's should have a Page#forms that works like an array.

> **···**
>
> On Jan 31, 2007, at 8:44 AM, Andy Lester wrote:
> 
> > On Jan 31, 2007, at 5:18 AM, Peter Szinek wrote:
> > 
> > > My problem is that the from does \*not\* have a 'name' attribute in the html - so apparently I can not use that to locate it - therefore I would like to use 'id' - or what else should I do?
> > 
> > I haven't dug into the Ruby Mech yet, but the Perl version lets you pick the Nth form on the page.

---

<div class="post-metadata">

**Author:** ![Peter\_Szinek3](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Peter\_Szinek3](https://rubytalk.org/u/Peter_Szinek3)\
**Post date:** [31 January 2007 14:11 UTC](https://rubytalk.org/t/mechanize-find-form-with-id-rather-than-name/34960/5 "2007-01-31T14:11:44Z")

</div>

Mat Schaffer wrote:

> > > My problem is that the from does \*not\* have a 'name' attribute in the html - so apparently I can not use that to locate it - therefore I would like to use 'id' - or what else should I do?
> > 
> > I haven't dug into the Ruby Mech yet, but the Perl version lets you pick the Nth form on the page.
> 
> Ruby's should have a Page#forms that works like an array.

Yeah, you can do

my\_form = doc.forms[0]

but all I have is the id attribute of the form I am looking for. (i.e. not the index of the form I am looking for).

So currently I am looping through all the forms and comparing the current form's id (through form\_node.attributes['id']) with the id I am looking for.

Is there a better way to do this? Or is this OK?

Peter

> **···**
>
> > On Jan 31, 2007, at 8:44 AM, Andy Lester wrote:
> > 
> > > On Jan 31, 2007, at 5:18 AM, Peter Szinek wrote:
> 
> \_\_  
> [http://www.rubyrailways.com](http://www.rubyrailways.com)
