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

I'm trying to get a little more familiar with the WWW::Mechanize module and have the following situation...I'm able to get a specific URL, but I'm trying to click a link on the page that uses javascript behind the scenes and two hidden fields. I'm able to get the required data for the two hidden fields but cannot submit the form for some reason. Can anyone tell me what I'm doing wrong? This is the script I'm using
use strict; use warnings; use WWW::Mechanize; use Data::Dumper; my $mech = WWW::Mechanize->new(); my $url = 'http://myspace.com/Modules/ViewFriends/FriendsView.aspx?%3f +fuseacion=user.viewfriends&friendID=' . 16193600; $mech->get( $url ); #print $mech->content(); # my $links = $mech->find_all_links(); # print Dumper($links); my $next_link = $mech->find_link( text_regex => qr/Next/i ); if($next_link) { my $url = $next_link->url(); if($url =~ /javascript:__doPostBack\('(.*)','(.*)'\)/) { my $target = $1; print "TARGET: $target\n"; my $argument = $2; print "ARGUMENT: $argument\n"; $mech->submit_form( { form_name => 'aspnetForm', fields_ref => { '__EVENTTARGET' => $target, '__EVENTARGUMENT' => $argument, }, } ) } } else { print "no more links on page\n"; }
Thanks for the help :)

Replies are listed 'Best First'.
Re: Mechanize & doPostBack ?
by gam3 (Curate) on Jan 13, 2007 at 07:20 UTC
    WWW::Mechanize does not know anything about JavaScript. You will need to parse any onclick type actions yourself and do the appropriate actions.
    -- gam3
    A picture is worth a thousand words, but takes 200K.