Mechanize

I’m trying to figure out how to use Mechanize to get the data out of a
select list in a page. The docs aren’t too clear on the steps needed
to do that.

Anyone have a good reference for me to use so that I can do this?

Thanks.

Mike B.

On 8/4/06, barjunk [email protected] wrote:

I’m trying to figure out how to use Mechanize to get the data out of a
select list in a page. The docs aren’t too clear on the steps needed
to do that.

Anyone have a good reference for me to use so that I can do this?

Not a reference, but here is some code from my freshmeat automation
that deals with select lists in a form field

s = form.field(“add_release[release_focus_name]”)
s.value = s.options[5].value

t = form.field(“add_release[hide_from_frontpage]”)
t.value = t.options[0].value

Gregory B. wrote:

On 8/4/06, barjunk [email protected] wrote:

[snip]

Not a reference, but here is some code from my freshmeat automation
that deals with select lists in a form field

s = form.field(“add_release[release_focus_name]”)
s.value = s.options[5].value

t = form.field(“add_release[hide_from_frontpage]”)
t.value = t.options[0].value

what do add_release[release_focus_name] and the add_release etc…stand
for?

It’s tough to understand without context.

Mike B.

barjunk wrote:

I’m trying to figure out how to use Mechanize to get the data out of a
select list in a page. The docs aren’t too clear on the steps needed
to do that.

Anyone have a good reference for me to use so that I can do this?

Thanks.

Mike B.

Can you just parse the html?

On Sat, Aug 05, 2006 at 04:20:09PM +0900, barjunk wrote:

Gregory B. wrote:

On 8/4/06, barjunk [email protected] wrote:

[snip]

Not a reference, but here is some code from my freshmeat automation
that deals with select lists in a form field

s = form.field(“add_release[release_focus_name]”)

The variable “s” is a SelectList class which represents a “select” html
tag.

s.value = s.options[5].value

A SelectList class has many Options associated with it. An Option
represents an “option” tag. Here Gregory is picking one of the options
and setting it as the value for this select list.

t = form.field(“add_release[hide_from_frontpage]”)
t.value = t.options[0].value

what do add_release[release_focus_name] and the add_release etc…stand
for?

“add_release[release_focus_name]” is the name of the select field.
Gregory is probably dealing HTML that looks something like this:

Option 1 Option 2 Option 3 Option 4 Option 5 Option 6

If you wanted to look at the text for each option, your code might look
something like this (using the variables Gregory had):

s.options.each { |o| puts “Text: ‘#{o.text}’ Value: ‘#{o.value}’” }

Make sure to check out the RDoc for WWW::Mechanize::SelectList and
WWW::Mechanize::Option at http://mechanize.rubyforge.org/

I hope this helps!

–Aaron

Aaron P. wrote:

On Sat, Aug 05, 2006 at 04:20:09PM +0900, barjunk wrote:
[snip]

s.options.each { |o| puts “Text: ‘#{o.text}’ Value: ‘#{o.value}’” }

Make sure to check out the RDoc for WWW::Mechanize::SelectList and
WWW::Mechanize::Option at http://mechanize.rubyforge.org/

Aaron, this helps tremendously! It would be awesome if these kind of
examples were in the api docs mentioned above. As it stands, the
example :

selectlist.value = selectlist.options.first.value

gives no context whatsoever and being new to both ruby and web
programming, I’m at a bit of a disadvantage.

Maybe a hint about how to go through the API to get the answer for
myself? :slight_smile:

One additional question I have is, what if there is more than one form
on the page?

Mechanize is awesome stuff, and I have already leveraged it to some
degree. Thanks for the additional help.

Mike B.

Aaron P. wrote:

On Sun, Aug 06, 2006 at 06:05:04AM +0900, barjunk wrote:
[snip]

myself? :slight_smile:

page.forms.with.name(‘foo’).first
page.forms.name(‘foo’).first

Take a look at the “Google” example in the RDoc example page. The
EXAMPLES Rdoc, and the NOTES Rdoc should be pretty helpful.

This was helpful also, thanks. I hadn’t thought of reading the NOTES
Rdoc, so thanks for that idea as well.

I’m not sure how the “Google” example helps me, I must be missing
something obvious…in addition to being new to ruby, I’m also new to
HTML and that might make more “obvious” things not so obvious. :slight_smile:

Thanks again Aaron.

I really enjoy using Mechanize…it saved me tons of time.

Mike B.

On Sun, Aug 06, 2006 at 06:05:04AM +0900, barjunk wrote:
[snip]

Aaron, this helps tremendously! It would be awesome if these kind of
examples were in the api docs mentioned above. As it stands, the
example :

selectlist.value = selectlist.options.first.value

gives no context whatsoever and being new to both ruby and web
programming, I’m at a bit of a disadvantage.

Agreed. This is not very intuitive, which is something I believe I’ve
fixed in 0.5.2 (which isn’t released yet). In 0.5.2 you’ll be able to
say:

selectlist.options.first.click
or
selectlist.options.first.select

Maybe a hint about how to go through the API to get the answer for
myself? :slight_smile:

Check out the EXAMPLES file. I’ve also tried to put examples in many
classes, though I will add more for the next release. I’ve also got a
few one liners in my blog:

http://tenderlovemaking.com/2006/05/26/mechanize-one-liners/

One additional question I have is, what if there is more than one form
on the page?

Forms are just stored as an array on the Page object returned to you by
mechanize. You can treat them like an array, or better yet use the name
of the form to find the right one to deal with.

To find the first form, you could do either of these:

page.forms.first
page.forms[0]

To find a form with the name “foo”, you can do either of these:

page.forms.with.name(‘foo’).first
page.forms.name(‘foo’).first

Take a look at the “Google” example in the RDoc example page. The
EXAMPLES Rdoc, and the NOTES Rdoc should be pretty helpful.

–Aaron

This is what worked for me:

page = agent.get(‘http://’ + server + ‘/admin/users.html?Add’)
form = page.forms.first
myselectlist = form.fields.first.options

I think I could also do:

myselectlist = form.field("").options

is a reference to the actual field from the select html
tag.

Having the HTML snippet was really helpful in getting it right.

Thanks again.

Mike B.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs