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

P

Peter Szinek

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?

Thanks,
Peter

__
http://www.rubyrailways.com
 
A

Andy Lester

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.
 
M

Mat Schaffer

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.
 
P

Peter Szinek

Mat said:
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

__
http://www.rubyrailways.com
 
L

Logan Capaldo

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 ...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,133
Latest member
MDACVReview
Top