mechanize:Mechanize#submit
以下のような 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])} " end print "</body></html>\n"
require 'mechanize' agent = Mechanize.new page = agent.get('http://127.0.0.1:8080/test_form00.html') page.forms.first.field_with(:name => 'select1').options[1].select page.forms.first.checkbox_with(:name => 'checkbox1').check page.forms.first.radiobuttons_with(:name => 'radio1')[1].check page.forms.first.field_with(:name => 'textarea1').value = "こんにちは" agent.submit(page.forms.first, page.forms.first.buttons.first) puts agent.page.root
で、
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head></head> <body>text1 : <br> radio1 : 女<br> checkbox1 : CellPhone<br> select1 : みかん<br> textarea1 : こんにちは<br> </body> </html>