mechanize:Mechanize::Form#add_field!
以下を確認
lib/mechanize/form.rb
def add_field!(field_name, value = nil) fields << Field.new({'name' => field_name}, value) end
def []=(field_name, value) f = field(field_name) if f.nil? add_field!(field_name, value) else f.value = value end end
ということは、以下のような HTML で
<html> <head> <title>form test</title> </head> <body> <form method="post" action="cgi/foo00.cgi" name="form1"> </form> <form method="post" action="cgi/foo01.cgi" name="form2"> </form> </body> </html>
>> require 'mechanize' => true >> agent = Mechanize.new => #<Mechanize:0xb7495540 ... >> page = agent.get('http://127.0.0.1:8080/test01_form.html') => #<Mechanize::Page {url #<URI::HTTP:0xb7491c88 URL:http://127.0.0.1:8080/test01_form.html>} {meta} {title "form test"} {iframes} {frames} {links} {forms #<Mechanize::Form {name "form1"} {method "POST"} {action "cgi/foo00.cgi"} {fields} {radiobuttons} {checkboxes} {file_uploads} {buttons}> #<Mechanize::Form {name "form2"} {method "POST"} {action "cgi/foo01.cgi"} {fields} {radiobuttons} {checkboxes} {file_uploads} {buttons}>}> >> f = page.form('form1') ... >> f['hoge'] => nil >> f['hoge'] = 'aaa' => "aaa" >> f['hoge'] => "aaa" >> f => #<Mechanize::Form {name "form1"} {method "POST"} {action "cgi/foo00.cgi"} {fields #<Mechanize::Form::Field:0xb747fe98 @name="hoge", @node={"name"=>"hoge"}, @value="aaa">} {radiobuttons} {checkboxes} {file_uploads} {buttons}>