SelectList with Mechanize in Ruby
Asked Answered
H

5

5

I'm trying to set the value of a select list using Mechanize with Ruby. I can navigate to the page with the select list, grab the form using the .form method, and find the select list.

report_form =page.form('form1')
pp report_form.field_with(:name => "report_type")

Correctly returns the right object.

However, I'm still unable to set the value of this field! I've tried:

report_form.field_with(:name => "report_type").options.first.select
report_form.field_with(:name => "report_type").options[1].select
report_form.field_with(:name => "report_type").value = "Foo"

But when I then do:

pp report_form.field_with(:name => "report_type")

The value field is still empty.

Is there something I'm missing? Tips? Tricks? Better Mechanize docs than what live at http://mechanize.rubyforge.org?

Thanks!

Edit: The relevant HTML is: The relevant HTML is:

<TD>
<select id="report_type" name="report_type">
    <option value="Foo1">Opt 1</option>
    <option value="Foo2">Opt 2</option>
    <option value="Foo3">Opt 3</option>
</select></TD>
Hyperplane answered 26/3, 2012 at 16:25 Comment(2)
The report_form.field_with(:name => "report_type").value = "Foo" should work for I'm understanding. The only to check this is to see the actual webpage.Spoilsman
I'm also unable to select any <option> in the HTML, but if I set the <select>'s value to an option not in the HTML it works fine. MEH. Did you ever resolve this?Tlaxcala
C
7

Try this

report_form.field_with(:name => "report_type").option_with(:value => "Foo").click
# now report_form.field_With(:name => "report_type").value should bee "Foo"

(via 1, 2)

Chibouk answered 26/3, 2012 at 18:54 Comment(1)
are you looking at .query_value ?Chibouk
R
3

It's usually good enough to do:

report_form["report_type"] = "Foo"
Robinia answered 27/3, 2012 at 1:20 Comment(0)
C
1

i ran into this same issue, nothing works for me either, but id like to clarify that im able to set the value to anything besides select options.

    report_form.field_with(:name => "report_type").value = "Foo1"
    report_form["report_type"]
    => "Foo1"
    report_form.field_with(:name => "report_type").value
    => "Foo1"
    report_form.field_with(:name => "report_type")
    => [selectlist:0x7c08ada type:  name: "report_type" value: []]

after submiting the form, the select is treated as empty, however if i do

    report_form.field_with(:name => "report_type").value = "anything not in the options"
    report_form.field_with(:name => "report_type")
    => [selectlist:0x7c08ada type:  name: "report_type" value: ["anything not in the options"]]
Chapple answered 20/2, 2013 at 17:49 Comment(1)
Did you ever resolve this issue? I'm having the exact same problem, and setting the <select>'s value to anything not in the options works :|Tlaxcala
F
0

Foo is not in the select list, i think if you change it to Foo1 (or the others) it should work!?

Federicofedirko answered 20/3, 2013 at 15:57 Comment(0)
T
0

It actually turned out to be a bug in the Mechanize gem. Make sure you're using v 0.6.0 or newer.

Tlaxcala answered 14/5, 2013 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.