# Mechanize

**URL:** <https://www.ruby-forum.com/t/mechanize/101471>\
**Category:** Ruby\
**Created:** [August 5, 2006, 5:11am UTC](https://www.ruby-forum.com/t/mechanize/101471 "2006-08-05T05:11:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [August 5, 2006, 5:11am UTC](https://www.ruby-forum.com/t/mechanize/101471/1 "2006-08-05T05:11:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [August 5, 2006, 5:38am UTC](https://www.ruby-forum.com/t/mechanize/101471/2 "2006-08-05T05:38:42Z")

</div>

On 8/4/06, barjunk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [August 5, 2006, 9:20am UTC](https://www.ruby-forum.com/t/mechanize/101471/3 "2006-08-05T09:20:53Z")

</div>

Gregory B. wrote:

> On 8/4/06, barjunk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [August 5, 2006, 5:50am UTC](https://www.ruby-forum.com/t/mechanize/101471/4 "2006-08-05T05:50:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [August 5, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/mechanize/101471/5 "2006-08-05T20:57:28Z")

</div>

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

> Gregory B. wrote:
> 
> > On 8/4/06, barjunk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://mechanize.rubyforge.org/)

I hope this helps!

–Aaron

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [August 5, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/mechanize/101471/6 "2006-08-05T23:06:37Z")

</div>

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/](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? 🙂

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.

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [August 6, 2006, 8:22am UTC](https://www.ruby-forum.com/t/mechanize/101471/7 "2006-08-06T08:22:29Z")

</div>

Aaron P. wrote:

> On Sun, Aug 06, 2006 at 06:05:04AM +0900, barjunk wrote:  
> [snip]
> 
> > myself? 🙂
> 
> 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. 🙂

Thanks again Aaron.

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

Mike B.

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [August 5, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/mechanize/101471/8 "2006-08-05T23:48:40Z")

</div>

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? 🙂

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/](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

---

<div class="post-metadata">

**Author:** ![barjunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@barjunk](https://www.ruby-forum.com/u/barjunk)\
**Post date:** [August 8, 2006, 2:37am UTC](https://www.ruby-forum.com/t/mechanize/101471/9 "2006-08-08T02:37:36Z")

</div>

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.
