mechanize:GUIDE:Advanced Form Techniques

以下のような HTML と

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

<!-- from http://www.kanzaki.com/docs/html/htminfo31.html -->

<form method="post" action="cgi/test_form00.cgi" name="form1">
 <p>
 氏名:<input type="text" name="text1" />
 性別:
 <input type="radio" name="radio1" value="男" checked="checked" /> 男 
 <input type="radio" name="radio1" value="女" /> 女
 所持品:
 <input type="checkbox" name="checkbox1" value="CellPhone" /> 携帯電話
 <input type="checkbox" name="checkbox1" value="Car" /> 自動車
 <input type="checkbox" name="checkbox1" value="Cottage" /> 別荘 
 好きな果物:
 <select name="select1">
   <option>りんご</option>
   <option>みかん</option>
   <option>バナナ</option>
   <option>パイナップル</option>
 </select>
 <textarea name="textarea1" rows="4" cols="40">自由に意見を記述してください</textarea>
 <input type="submit" value="送信" /> 
 <input type="reset" value="取り消し" />
 </p>
</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", "radio1", "checkbox1", "select1", "textarea1"].each do |x|
  puts "#{x} : #{CGI.escapeHTML(c[x])}<br>"
end

print "</body></html>\n"
>> require 'mechanize'
=> true
>> agent = Mechanize.new
=> #<Mechanize:0xb74b77f8 
  ...
>> page = agent.get('http://127.0.0.1:8080/test_form00.html')
=> #<Mechanize::Page
 {url #<URI::HTTP:0xb74b3f40 URL:http://127.0.0.1:8080/test_form00.html>}
  ...
>> form = page.form('form1')
=> #<Mechanize::Form
 {name "form1"}
 {method "POST"}
 {action "cgi/test_form00.cgi"}
 {fields
  ...
>> form.field_with(:name => 'select1').options[1].select
=> true
>> agent.submit(form)
=> #<Mechanize::Page
 {url #<URI::HTTP:0xb7425d08 URL:http://127.0.0.1:8080/cgi/test_form00.cgi>}
  ...
>> agent.page
=> #<Mechanize::Page
 {url #<URI::HTTP:0xb74884bc URL:http://127.0.0.1:8080/cgi/test_form00.cgi>}
>> agent.page.root
=> #<Nokogiri::HTML::Document:
  ...
>> puts agent.page.root.children[1].children
<head></head>
<body>text1 : <br>
radio1 : 男<br>
checkbox1 : <br>
select1 : みかん<br>
textarea1 : 自由に意見を記述してください<br>
</body>
=> nil

本当は irb 上では、日本語はうまく表示されなかったんだけど

require 'mechanize'

agent = Mechanize.new
page = agent.get('http://127.0.0.1:8080/test_form00.html')
form = page.form('form1')

form.field_with(:name => 'select1').options[1].select
agent.submit(form)
puts agent.page.root.children[1].children; puts

form.checkbox_with(:name => 'checkbox1').check
agent.submit(form)
puts agent.page.root.children[1].children; puts

form.radiobuttons_with(:name => 'radio1')[1].check
agent.submit(form)
puts agent.page.root.children[1].children; puts

form.field_with(:name => 'textarea1').value = "こんにちは"
agent.submit(form)
puts agent.page.root.children[1].children

で、

<head></head>
<body>text1 : <br>
radio1 : 男<br>
checkbox1 : <br>
select1 : みかん<br>
textarea1 : 自由に意見を記述してください<br>
</body>

<head></head>
<body>text1 : <br>
radio1 : 男<br>
checkbox1 : CellPhone<br>
select1 : みかん<br>
textarea1 : 自由に意見を記述してください<br>
</body>

<head></head>
<body>text1 : <br>
radio1 : 女<br>
checkbox1 : CellPhone<br>
select1 : みかん<br>
textarea1 : 自由に意見を記述してください<br>
</body>

<head></head>
<body>text1 : <br>
radio1 : 女<br>
checkbox1 : CellPhone<br>
select1 : みかん<br>
textarea1 : こんにちは<br>
</body>
  • checkbox の名前が同じなんだけど、最初のでないものを選択するにはどうしたら?
  • そもそも CGI として同名の checkbox の値を取得するには?