Niner710 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use WWW::mechanize so that I can go to a website and check the radio button. The value field of the html page I am using constantly changes everytime I load the page. There is only one radio button and was wondering how I would turn that radio button on. I saw $mech->set_visible was a possibility, but was wondering if anyone can give me feedback on how to do this. Thank you.

Replies are listed 'Best First'.
Re: click radio button with WWW::Mechanize
by Anonymous Monk on Nov 17, 2007 at 10:50 UTC

      I just found this very old node by doing some searches on PM regarding a similar problem with mechanize.

      Funny thing is, that whilst the answer above does address the issue at hand (and also proves the laziness of people towards searching before asking), none of the listed nodes above actually provide any clear and simple answer as to the questions posted by any of the OPs!

      Anyway, I found it quite funny and peculiar, so here is the clear and simple answer:

      In WWW::Mechanize radio buttons can be set just like you would set any regular text input field, but you must know the exact value of the radio button you want to select beforehand. They don't work like selects and they don't work by clicking. See examples below:

      # this 'clicks' the $rb_value button in rb_name group $mech->set_fields( rb_name => $rb_value, ); # this will work as well $mech->submit_form( form_number => 1, fields => { rb_name => $rb_value, }, );
        Yeah, most people eventually figure out set_fields or submit_form ... and realize just what a PITA some of the methods can be (esp HTML::Form )

        Here is how I test these things

        #!/usr/bin/perl -- use strict; use warnings; use WWW::Mechanize 1.66; use URI::file; Main( @ARGV ); exit( 0 ); sub Main { my $ua = WWW::Mechanize->new( autocheck => 1, ); $ua->timeout(0.00000000001); $ua->get( URI::file->new(__FILE__)->abs( URI::file->cwd ) ); $ua->update_html( <<'HTML' ); <html> <head> <title> test.html : localhost form </title> </head> <body> <base href="http://localhost/"> <form method="POST" action="http://localhost/"> <input type="hidden" name=r0 value=0 disabled>zero <input type=radio name=r1 value=1 disabled>one <input type=radio name=r1 value=2>two <input type=radio name=r1 value=3>three <input type=radio name=r2 value=1>one <input type=radio name=r2 value=2 disabled>two <input type=radio name=r2 value=3>three <input type="submit"> </form> </body> </html> HTML print $ua->dump_forms,"\n\n"; $ua->set_fields( qw' r1 three r2 three r3 three '); print $ua->dump_forms,"\n\n"; $ua->set_fields( qw' r2 7 '); print $ua->dump_forms,"\n\n"; $ua->add_header( Referer => undef ); $ua->add_handler( "request_send", sub { $_[0]->dump; return; }, m_method => 'POST' ); $ua->submit(0); $ua->delete_header( 'Referer' ); } __END__ $ perl mechanize.radio.pl POST http://localhost/ r0=0 (hidden disabled readonly) r1=<UNDEF> (radio) [-1/one|2/two|3/three] r2=<UNDEF> (radio) [1/one|-2/two|3/three] <NONAME>=<UNDEF> (submit) POST http://localhost/ r0=0 (hidden disabled readonly) r1=3 (radio) [-1/one|2/two|*3/three] r2=3 (radio) [1/one|-2/two|*3/three] <NONAME>=<UNDEF> (submit) r3=three (text) POST http://localhost/ r0=0 (hidden disabled readonly) r1=3 (radio) [-1/one|2/two|*3/three] r2=7 (radio) [1/one|-2/two|:3/three] <NONAME>=<UNDEF> (submit) r3=three (text) POST http://localhost/ Accept-Encoding: gzip User-Agent: WWW-Mechanize/1.66 Content-Length: 18 Content-Type: application/x-www-form-urlencoded r1=3&r2=7&r3=three Error POSTing http://localhost/: Can't connect to localhost:80 (timeou +t) at mechanize.radio.pl line 63