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

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

Replies are listed 'Best First'.
Re^4: click radio button with WWW::Mechanize
by Aldebaran (Curate) on Jun 19, 2015 at 00:44 UTC

    I found this thread with a google search for "perl WWW::Mechanize radiobutton examples." I always read the ones for perlmonks first and found the links to other sources unhelpful. Using the syntax from above, I was able to use WWW::Mechanize to select a radio button, and supply a text with data that populates a new guess on the time at which the event I'm looking for is to occur. That event, btw, is the conjunction of venus and jupiter, which will happen in somewhere around 2 weeks from the writing of this node (6-18-15). I'm using John Walker's Fourmilab site and perl to determine when this will happen exactly.

    Walker's site makes a good guinea pig for WWW::Mechanize for several reasons, but let me name a few. This script will produce the same output in a time-invariant fashion. The html isn't written by a machine that cares nothing for legibility. There's no splash screens, adware, flash videos, or similar pests. One can easily change these values and focus on new astronomic events. My script:

    #! /usr/bin/perl use warnings; use strict; use 5.010; use WWW::Mechanize::GZip; use HTML::TableExtract qw(tree); my $site = 'http://www.fourmilab.ch/yoursky/cities.html'; my $mech = 'WWW::Mechanize::GZip'->new; $mech->get($site); $mech->follow_link( text => 'Portland OR' ); my $lub = 2457204.63659; #least upper bound my $glb = 2457207.63659; #greatest lower bound say $mech->dump_forms; $mech->set_fields( qw'date 2'); my $guess = median( $lub, $glb); say "guess is $guess"; $mech->set_fields( jd => $guess ); $mech->click_button(value => "Update"); say $mech->dump_forms; my $te = 'HTML::TableExtract'->new; $te->parse($mech->content); my $table = ($te->tables)[3]; my $table_tree = $table->tree; my $table_text = $table_tree->as_text; say "table text is $table_text"; my $venus = $table_tree->cell(4,1)->as_text; say "say venus is $venus"; my $jupiter = $table_tree->cell(7,1)->as_text; say "say jupiter is $jupiter"; sub median{ my ($upper, $lower) = @_; my $return = ($upper+$lower)/2.0; return $return; }

    Output:

    POST http://www.fourmilab.ch/cgi-bin/Yoursky [request] <NONAME>=Update (submit) date=0 (radio) [*0/Now|1/Universal time:| +2/Julian day:] utc=2015-06-18 23:44:00 (text) jd=2457192.48889 (text) lat=45°31'5" (text) ns=North (radio) [*North|South] lon=122°40'33" (text) ew=West (radio) [East|*West] coords=on (checkbox) [<UNDEF>/off|*on/Ecliptic +and equator] moonp=on (checkbox) [<UNDEF>/off|*on/Moon and +planets] deep=on (checkbox) [<UNDEF>/off|*on/Deep sky +objects of magnitude] deepm=2.5 (text) consto=on (checkbox) [<UNDEF>/off|*on/Outlines] constn=on (checkbox) [<UNDEF>/off|*on/Names] consta=<UNDEF> (checkbox) [*<UNDEF>/off|on/aligned w +ith horizon?] consts=<UNDEF> (checkbox) [*<UNDEF>/off|on/abbreviat +e?] constb=on (checkbox) [<UNDEF>/off|*on/Boundarie +s Stars: Show stars brighter than magnitude] limag=5.5 (text) starn=on (checkbox) [<UNDEF>/off|*on/Names for + magnitude] starnm=2.0 (text) starb=on (checkbox) [<UNDEF>/off|*on/Bayer/Fla +msteed codes for mag.] starbm=2.5 (text) flip=<UNDEF> (checkbox) [*<UNDEF>/off|on/Invert No +rth and South Image size:] imgsize=640 (text) fontscale=1.0 (text) scheme=0 (option) [*0/Colour|1/Black on whit +e background|2/White on black background|3/Night vision (red)] edump=<UNDEF> (checkbox) [*<UNDEF>/off|-xe/Echo ele +ments] elements= (textarea) guess is 2457206.13659 POST http://www.fourmilab.ch/cgi-bin/Yoursky [request] <NONAME>=Update (submit) date=2 (radio) [0/Now|1/Universal time:|* +2/Julian day:] utc=2015-07-02 15:16:41 (text) jd=2457206.13659 (text) lat=45° (text) ns=North (radio) [*North|South] lon=122° (text) ew=West (radio) [East|*West] coords=on (checkbox) [<UNDEF>/off|*on/Ecliptic +and equator] moonp=on (checkbox) [<UNDEF>/off|*on/Moon and +planets] deep=on (checkbox) [<UNDEF>/off|*on/Deep sky +objects of magnitude] deepm=2.5 (text) consto=on (checkbox) [<UNDEF>/off|*on/Outlines] constn=on (checkbox) [<UNDEF>/off|*on/Names] consta=<UNDEF> (checkbox) [*<UNDEF>/off|on/aligned w +ith horizon?] consts=<UNDEF> (checkbox) [*<UNDEF>/off|on/abbreviat +e?] constb=on (checkbox) [<UNDEF>/off|*on/Boundarie +s Stars: Show stars brighter than magnitude] limag=5.5 (text) starn=on (checkbox) [<UNDEF>/off|*on/Names for + magnitude] starnm=2.0 (text) starb=on (checkbox) [<UNDEF>/off|*on/Bayer/Fla +msteed codes for mag.] starbm=2.5 (text) flip=<UNDEF> (checkbox) [*<UNDEF>/off|on/Invert No +rth and South Image size:] imgsize=640 (text) fontscale=1.0 (text) scheme=0 (option) [*0/Colour|1/Black on whit +e background|2/White on black background|3/Night vision (red)] edump=<UNDEF> (checkbox) [*<UNDEF>/off|-xe/Echo ele +ments] elements= (textarea) table text is  RightAscensionDeclinationDistance(AU)From 45°N 122°W:Al +titudeAzimuthSun6h 45m 22s+23° 1.5'1.01727.248&#8722;96.154UpMercury5 +h 17m 22s+21° 6.7'1.00041.481&#8722;78.217UpVenus9h 39m 50s+14° 20.7' +0.504&#8722;8.079&#8722;119.724SetMoon19h 13m 12s&#8722;17° 26.2'58.8 + ER&#8722;18.65783.705SetMars6h 23m 45s+24° 5.1'2.58531.733&#8722;93. +449UpJupiter9h 38m 6s+15° 1.7'6.094&#8722;7.283&#8722;119.835SetSatur +n15h 49m 5s&#8722;17° 53.4'9.200&#8722;53.265127.849SetUranus1h 15m 5 +1s+7° 19.6'20.14051.60213.737UpNeptune22h 46m 15s&#8722;8° 39.6'29.43 +222.30450.179UpPluto19h 0m 30s&#8722;20° 42.1'31.888&#8722;23.07383.2 +56Set say venus is 9h 39m 50s say jupiter is 9h 38m 6s

    I've already thanked the monks who wrote this thread in the form of my upvote, but I'd also like to thank all those who help me indulge my twin hobbies of skywatching and perl.