Mechanize

B

barjunk

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

Gregory Brown

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
 
W

William James

barjunk said:
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?
 
B

barjunk

Gregory said:
[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.
 
A

Aaron Patterson

Gregory said:
[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:

<select name="add_release[release_focus_name]">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
</select>

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
 
B

barjunk

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

<select name="add_release[release_focus_name]">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
</select>

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/

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

Aaron Patterson

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/
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
 
B

barjunk

Aaron said:
On Sun, Aug 06, 2006 at 06:05:04AM +0900, barjunk wrote: [snip]
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/
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.
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.
 
B

barjunk

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("<selectname>").options


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

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top