mechanize:Mechanize::Form#, #=

以下のような HTML で

<html>
<head>
<title>form test</title>
</head>
<body>

<form method="get" action="cgi/test_form01.cgi" name="form1">
  <input type="text" name="text1" value="FOO" />
  <textarea name="textarea1" rows="4" cols="40">foo</textarea>
  <input type="submit" value="Send" />
</form>

</body>
</html>

以下のような確認用 CGI を用意し、webrick で確認

#!/usr/local/ruby-1.8.7/bin/ruby

require "cgi"

print "Content-Type: text/html\n\n"
print "<html><head></head><body>"

c = CGI.new

["text1", "textarea1"].each do |x|
  puts "#{x} : #{CGI.escapeHTML(c[x])}<br>"
end

print "</body></html>\n"
>> require 'mechanize'
=> true
>> agent = Mechanize.new
=> #<Mechanize:0xb74c0538 ...
>> page = agent.get('http://127.0.0.1:8080/test16_form.html')
=> #<Mechanize::Page
 {url #<URI::HTTP:0xb74bcc80 URL:http://127.0.0.1:8080/test16_form.html>}
 {meta}
 {title "form test"}
 {iframes}
 {frames}
 {links}
 {forms
  #<Mechanize::Form
   {name "form1"}
   {method "GET"}
   {action "cgi/test_form01.cgi"}
   {fields
    #<Mechanize::Form::Text:0xb74b4210
     @name="text1",
     @node=
      #(Element:0x..fdba5a1d0 {
        name = "input",
        attributes = [
          #(Attr:0x..fdba587d6 { name = "type", value = "text" }),
          #(Attr:0x..fdba587cc { name = "name", value = "text1" }),
          #(Attr:0x..fdba587c2 { name = "value", value = "FOO" })]
        }),
     @value="FOO">
    #<Mechanize::Form::Field:0xb74b393c
     @name="textarea1",
     @node=
      #(Element:0x..fdba59cda {
        name = "textarea",
        attributes = [
          #(Attr:0x..fdba56ea4 { name = "name", value = "textarea1" }),
          #(Attr:0x..fdba56e9a { name = "rows", value = "4" }),
          #(Attr:0x..fdba56e90 { name = "cols", value = "40" })],
        children = [ #(Text "foo")]
        }),
     @value="foo">}
   {radiobuttons}
   {checkboxes}
   {file_uploads}
   {buttons
    #<Mechanize::Form::Submit:0xb74b40a8
     @name=nil,
     @node=
      #(Element:0x..fdba5a180 {
        name = "input",
        attributes = [
          #(Attr:0x..fdba5500e { name = "type", value = "submit" }),
          #(Attr:0x..fdba55004 { name = "value", value = "Send" })]
        }),
     @value="Send">}>}>

>> f = page.form('form1')
=> #<Mechanize::Form
 {name "form1"}
 {method "GET"}
 {action "cgi/test_form01.cgi"}
 {fields
  #<Mechanize::Form::Text:0xb74b4210
   @name="text1",
   @node=
    #(Element:0x..fdba5a1d0 {
      name = "input",
      attributes = [
        #(Attr:0x..fdba587d6 { name = "type", value = "text" }),
        #(Attr:0x..fdba587cc { name = "name", value = "text1" }),
        #(Attr:0x..fdba587c2 { name = "value", value = "FOO" })]
      }),
   @value="FOO">
  #<Mechanize::Form::Field:0xb74b393c
   @name="textarea1",
   @node=
    #(Element:0x..fdba59cda {
      name = "textarea",
      attributes = [
        #(Attr:0x..fdba56ea4 { name = "name", value = "textarea1" }),
        #(Attr:0x..fdba56e9a { name = "rows", value = "4" }),
        #(Attr:0x..fdba56e90 { name = "cols", value = "40" })],
      children = [ #(Text "foo")]
      }),
   @value="foo">}
 {radiobuttons}
 {checkboxes}
 {file_uploads}
 {buttons
  #<Mechanize::Form::Submit:0xb74b40a8
   @name=nil,
   @node=
    #(Element:0x..fdba5a180 {
      name = "input",
      attributes = [
        #(Attr:0x..fdba5500e { name = "type", value = "submit" }),
        #(Attr:0x..fdba55004 { name = "value", value = "Send" })]
      }),
   @value="Send">}>

>> f['text1']
=> "FOO"
>> f['textarea1']
=> "foo"
>> f['text1'] = "FOO FOO"
=> "FOO FOO"
>> f['textarea1'] = "foo foo"
=> "foo foo"
>> f['text1']
=> "FOO FOO"
>> f['textarea1']
=> "foo foo"
>> agent.submit(f)
=> #<Mechanize::Page
 {url
  #<URI::HTTP:0xb74921b0 URL:http://127.0.0.1:8080/cgi/test_form01.cgi?text1=FOO+FOO&textarea1=foo+foo>}
 {meta}
 {title nil}
 {iframes}
 {frames}
 {links}
 {forms}>

>> agent.page.root.children[1]
=> #<Nokogiri::XML::Element:0x..fdba46c3e name="html" children=[#<Nokogiri::XML::Element:0x..fdba44fce name="head">, #<Nokogiri::XML::Element:0x..fdba44fa6 name="body" children=[#<Nokogiri::XML::Text:0x..fdba44db2 "text1 : FOO FOO">, #<Nokogiri::XML::Element:0x..fdba44d8a name="br">, #<Nokogiri::XML::Text:0x..fdba44d26 "\ntextarea1 : foo foo">, #<Nokogiri::XML::Element:0x..fdba44c4a name="br">]>]>