Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^2: click radio button with WWW::Mechanize

by ait (Friar)
on Apr 20, 2011 at 13:24 UTC ( #900338=note: print w/ replies, xml ) Need Help??

in reply to Re: click radio button with WWW::Mechanize
in thread click radio button with WWW::Mechanize

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, }, );

Comment on Re^2: click radio button with WWW::Mechanize
Download Code
Re^3: click radio button with WWW::Mechanize
by Anonymous Monk on Apr 21, 2011 at 13:02 UTC
    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 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 line 63

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://900338]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2015-05-30 10:56 GMT
Find Nodes?
    Voting Booth?

    In my home, the TV remote control is ...

    Results (594 votes), past polls