Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: WWW::Mechanize clicking checkboxes

by Anonymous Monk
on Mar 21, 2011 at 19:17 UTC ( [id://894600]=note: print w/replies, xml ) Need Help??


in reply to Re^4: WWW::Mechanize clicking checkboxes
in thread WWW::Mechanize clicking checkboxes

I concur wholeheartedly with Corion, Ovid's CGI Course is a good start

Also, for every question like this you have, you should write a small program to answer it, like this

#!/usr/bin/perl -- use strict; use warnings; use WWW::Mechanize 1.66; use URI::file; my $ua = WWW::Mechanize->new( autocheck => 1, ); $ua->add_handler( "request_send", sub { $_[0]->dump; return; }, m_method => 'POST' ); $ua->timeout(1); #~ $ua->get( URI::file->new( 'test.html' )->abs(URI::file->cwd) ); $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='checkbox' name='CategoryId25' CHECKED /> <input type="checkbox" name="check" value="1111" >1111 <input type="checkbox" name="check" value="2222" CHECKED>2222 <input type="checkbox" name="check" value="3333" >3333 <input type="checkbox" name="check" value="4444" CHECKED>4444 <input type="checkbox" name="check" value="5555" >5555 <input id="enterbutton" type="submit" name="user_choice" value="Enter" + /> <input type="submit" name="user_choice" value="Leave" /> </form> </body> </html> HTML print '# ', __LINE__, ' ', '#' x 33, "\n"; eval { $ua->submit_form( form_number => 0, fields => {qw' user_choice Enter '}, ); } or print "\n", __LINE__, " $@"; print '# ', __LINE__, ' ', '#' x 33, "\n"; eval { $ua->back; # if autocheck => 0 $ua->form_number(0); #~ $ua->set_fields( check => [ 3333, 5555 ] );# error, checkboxes not +fields $ua->tick( check => 2222 => 0 ); # untick/uncheck $ua->tick( check => 3333 ); # tick/check $ua->untick( check => 4444 ); # uncheck $ua->tick( check => 5555 => 1 ); # check $ua->click_button( value => 'Enter' ); # submit } or print "\n", __LINE__, " $@"; print '# ', __LINE__, ' ', '#' x 33, "\n"; eval { $ua->back; # if autocheck => 0 $ua->submit_form( form_number => 0, fields => { qw' user_choice Enter ', check => 1111, check => 5555, check => 6666, }, ); } or print "\n", __LINE__, " $@"; print $ua->dump_forms,"\n\n"; __END__ $ perl mechanize.inline.submit_form3.pl # 37 ################################# POST http://localhost/ Accept-Encoding: gzip Referer: file:////~/temp/mechanize.inline.submit_form3.pl User-Agent: WWW-Mechanize/1.66 Content-Length: 37 Content-Type: application/x-www-form-urlencoded CategoryId25=on&check=2222&check=4444 43 Error POSTing http://localhost/: Can't connect to localhost:80 (ti +meout) at mechanize.inline.submit_form3.pl line 39 # 45 ################################# POST http://localhost/ Accept-Encoding: gzip Referer: file:////~/temp/mechanize.inline.submit_form3.pl User-Agent: WWW-Mechanize/1.66 Content-Length: 55 Content-Type: application/x-www-form-urlencoded CategoryId25=on&check=3333&check=5555&user_choice=Enter 56 Error POSTing http://localhost/: Can't connect to localhost:80 (ti +meout) at mechanize.inline.submit_form3.pl line 55 # 58 ################################# POST http://localhost/ Accept-Encoding: gzip Referer: file:////~/temp/mechanize.inline.submit_form3.pl User-Agent: WWW-Mechanize/1.66 Content-Length: 48 Content-Type: application/x-www-form-urlencoded CategoryId25=on&check=6666&check=2222&check=4444 70 Error POSTing http://localhost/: Can't connect to localhost:80 (ti +meout) at mechanize.inline.submit_form3.pl line 61 POST http://localhost/ CategoryId25=on (checkbox) [<UNDEF>/off|*on] check=6666 (checkbox) [:<UNDEF>/off|1111] check=2222 (checkbox) [<UNDEF>/off|*2222] check=<UNDEF> (checkbox) [*<UNDEF>/off|3333] check=4444 (checkbox) [<UNDEF>/off|*4444] check=<UNDEF> (checkbox) [*<UNDEF>/off|5555] user_choice=Enter (submit) user_choice=Leave (submit)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-12-13 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (67 votes). Check out past polls.