mechanize:Mechanize::Page#labels_hash
以下のような HTML で
<html> <head> <title>label test</title> </head> <body> <form method="post" action="cgi/test_form00.cgi" name="form1"> <input type="radio" name="radio1" id="r1" value="male" checked="checked"><label for="r1">Male</label> <input type="radio" name="radio1" id="r2" value="female"><label for="r2">Female</label> </form> </body> </html>
>> require 'mechanize' => true >> agent = Mechanize.new => #<Mechanize:0xb74782d8 ... >> page = agent.get('http://127.0.0.1:8080/test05_label.html') => #<Mechanize::Page {url #<URI::HTTP:0xb74749bc URL:http://127.0.0.1:8080/test05_label.html>} {meta} {title "label test"} {iframes} {frames} {links} {forms #<Mechanize::Form {name "form1"} {method "POST"} {action "cgi/test_form00.cgi"} {fields} {radiobuttons #<Mechanize::Form::RadioButton:0xb746c000 @checked=true, @name="radio1", @value="male"> #<Mechanize::Form::RadioButton:0xb746be98 @checked=false, @name="radio1", @value="female">} {checkboxes} {file_uploads} {buttons}>}> >> page.labels_hash => {"r2"=>#<Mechanize::Page::Label:0xb74663a8 @page=#<Mechanize::Page {url #<URI::HTTP:0xb74749bc URL:http://127.0.0.1:8080/test05_label.html>} {meta} {title "label test"} {iframes} {frames} {links} {forms #<Mechanize::Form {name "form1"} {method "POST"} {action "cgi/test_form00.cgi"} {fields} {radiobuttons #<Mechanize::Form::RadioButton:0xb746c000 @checked=true, @name="radio1", @value="male"> #<Mechanize::Form::RadioButton:0xb746be98 @checked=false, @name="radio1", @value="female">} {checkboxes} {file_uploads} {buttons}>}> , @text="Female", @node=#<Nokogiri::XML::Element:0x..fdba33206 name="label" attributes=[#<Nokogiri::XML::Attr:0x..fdba305b0 name="for" value="r2">] children=[#<Nokogiri::XML::Text:0x..fdba30394 "Female">]>>, "r1"=>#<Mechanize::Page::Label:0xb7466420 @page=#<Mechanize::Page {url #<URI::HTTP:0xb74749bc URL:http://127.0.0.1:8080/test05_label.html>} {meta} {title "label test"} {iframes} {frames} {links} {forms #<Mechanize::Form {name "form1"} {method "POST"} {action "cgi/test_form00.cgi"} {fields} {radiobuttons #<Mechanize::Form::RadioButton:0xb746c000 @checked=true, @name="radio1", @value="male"> #<Mechanize::Form::RadioButton:0xb746be98 @checked=false, @name="radio1", @value="female">} {checkboxes} {file_uploads} {buttons}>}> , @text="Male", @node=#<Nokogiri::XML::Element:0x..fdba33238 name="label" attributes=[#<Nokogiri::XML::Attr:0x..fdba2e33c name="for" value="r1">] children=[#<Nokogiri::XML::Text:0x..fdba2e120 "Male">]>>} >> page.labels_hash["r2"] => #<Mechanize::Page::Label:0xb74663a8 @page=#<Mechanize::Page {url #<URI::HTTP:0xb74749bc URL:http://127.0.0.1:8080/test05_label.html>} {meta} {title "label test"} {iframes} {frames} {links} {forms #<Mechanize::Form {name "form1"} {method "POST"} {action "cgi/test_form00.cgi"} {fields} {radiobuttons #<Mechanize::Form::RadioButton:0xb746c000 @checked=true, @name="radio1", @value="male"> #<Mechanize::Form::RadioButton:0xb746be98 @checked=false, @name="radio1", @value="female">} {checkboxes} {file_uploads} {buttons}>}> , @text="Female", @node=#<Nokogiri::XML::Element:0x..fdba33206 name="label" attributes=[#<Nokogiri::XML::Attr:0x..fdba305b0 name="for" value="r2">] children=[#<Nokogiri::XML::Text:0x..fdba30394 "Female">]>>