Mechanize

Discussion in 'Ruby' started by barjunk, Aug 5, 2006.

  1. barjunk

    barjunk Guest

    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.
    barjunk, Aug 5, 2006
    #1
    1. Advertising

  2. On 8/4/06, 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?


    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 Brown, Aug 5, 2006
    #2
    1. Advertising

  3. 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?
    William James, Aug 5, 2006
    #3
  4. barjunk

    barjunk Guest

    Gregory Brown wrote:
    > On 8/4/06, barjunk <> 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, Aug 5, 2006
    #4
  5. On Sat, Aug 05, 2006 at 04:20:09PM +0900, barjunk wrote:
    > Gregory Brown wrote:
    > > On 8/4/06, barjunk <> 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:

    <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
    Aaron Patterson, Aug 5, 2006
    #5
  6. barjunk

    barjunk Guest

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

    [snip]
    > > > 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.
    barjunk, Aug 5, 2006
    #6
  7. 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
    Aaron Patterson, Aug 5, 2006
    #7
  8. barjunk

    barjunk Guest

    Aaron Patterson wrote:
    > 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.
    barjunk, Aug 6, 2006
    #8
  9. barjunk

    barjunk Guest

    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.
    barjunk, Aug 8, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zoewu

    help with Perl Mechanize

    zoewu, Feb 27, 2004, in forum: Perl
    Replies:
    0
    Views:
    2,191
    zoewu
    Feb 27, 2004
  2. bruce
    Replies:
    1
    Views:
    287
    John J. Lee
    Jul 9, 2006
  3. bruce

    Mechanize-Browser question..

    bruce, Jul 8, 2006, in forum: Python
    Replies:
    1
    Views:
    369
    Tal Einat
    Jul 9, 2006
  4. bruce

    unistall python mechanize

    bruce, Jul 10, 2006, in forum: Python
    Replies:
    1
    Views:
    286
    John J. Lee
    Jul 10, 2006
  5. bruce
    Replies:
    0
    Views:
    1,341
    bruce
    Jul 10, 2006
Loading...

Share This Page