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

I'm a newbie Perl user, so I would appreciate any help you can provide. I am trying to log into a web site with Mechanize. I have determined that I can pass the username to the form, but not the password, nor can I click the "Continue" button. The x,y coordinates were obtained by inspecting the element console while I clicked the button.

Two questions: 1) can you help me enter the password 2) can you help me click the "Continue" button

url: https://apps.fcc.gov/redlight/login.cfm

I can pass the username via code and also via url by doing this:

url: https://apps.fcc.gov/redlight/login.cfm?loginFrn=username

Here is my code...you will notice many #'ed lines where you can see what I was trying:
#!/usr/bin/perl #fcc redlight check # use strict; use warnings; use WWW::Mechanize; use MIME::Lite;#for emailing use Net::SSLeay; use Crypt::SSLeay; use Getopt::Std; use HTTP::Cookies; #use JavaScript::SpiderMonkey; #use WWW::Scripter; #use WWW::Scripter::Plugin::JavaScript; my $mech = new WWW::Mechanize(autocheck=>1); $mech->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.7) Gec +ko/20050418 Firefox/1.0.3"); my $from = 'fromemail@whatever.com'; my $to = 'toemail@whatever.com'; my $cc = ''; my $subject = "RED LIGHT!!"; my $frn = "password"; my $pwd = "username"; my $url = "https://apps.fcc.gov/redlight/login.cfm"; my $message = "$frn appears to be in Red Light Status!\n\n$url"; my $path = "tmp"; my $filename = "test.html"; my $status = "STATUS: Green"; $mech->get( $url ); die sprintf(" crap(%d): %s ", $mech->status, $mech->content ) unless $mech->success; #$mech->submit_form( # form_name => 'login', # fields => { loginFrn=>$frn, password=>$pwd }, # fileds => { loginPassword=>$pwd }, # #button => 'submit', # ); $mech->submit_form( form_number => 1, with_fields => { loginFrn => $frn, loginPassword => $pwd, } ); #HOW THE FRIG DO YOU HIT THAT CONTINUE BUTTON?!? #$mech->click_button (value => "Continue"); #$mech->click( my $button [, 6, 28] ); #$mech->click_button (number=>1); #$mech->click_button (name => 'Continue'); #$mech->click_button (name => undef); #$mech->click( undef [, 6, 28] ); $mech->submit(); $mech->check_fields(); my $file = "/$path/$filename"; # create /tmp/test.html open(my $handle, ">", $file) || die "can't open $path: $!"; binmode($handle); # for raw; else set the encoding my $result = $mech->content; print $handle "$result\n"; close($handle) || die "can't close $path: $!"; #die; if ($mech->content =~ m/Invalid combination of/i) { print "login attempted, but failed\n"; die; } if ($mech->content =~ m/Red Light Display System Login/i) { print "Login not attempted?\n"; die; } die sprintf(" crap(%d): %s", $mech->status, $mech->content) unless $mech->success; if( $mech->content =~ m/delinquent/i ) { print "I win\n" } else { die "crap, didn't match green status. :-(\n"; }

Replies are listed 'Best First'.
Re: click button
by Corion (Patriarch) on Oct 31, 2015 at 20:24 UTC

    Looking at the HTML source of the "Continue" button:

    <input type="image" src="images/continue-blue.gif" width="73" height=" +17" alt="Continue" border="0" onclick="check_fields();">

    That button uses Javascript. WWW::Mechanize does not support Javascript.

    There are different approaches for you to proceed:

    You could look at the Javascript source code of that site and figure out what the Javascript does, and replicate that with Perl.

    You could use (for example) Firefox HTTP Live Headers to find out the HTTP requests that the browser makes in response to the button being clicked, and replicate them with Perl.

    You could automate a web browser that understands Javascript, like WWW::Mechanize::Firefox, WWW::Mechanize::PhantomJS and maybe others.

      I have the javascript that passed through the console. How do I "convert it" to Perl? Specifically, I believe this is the key part of the code:
      "postData": { "mimeType": "application/x-www-form-urlencoded", "text": "loginFrn=0123456789&loginPassword=PasswordX&x=28& +y=6", "params": [ { "name": "loginFrn", "value": "0123456789" }, { "name": "loginPassword", "value": "PasswordX" }, { "name": "x", "value": "28" }, { "name": "y", "value": "6" } ]
      I can give you the full code if that isn't correct, but if it is, what does the converted code look like?

        This looks more like data that happens to be readable as a JSON data structure than Javascript code. If you don't know Javascript, then you will need to find somebody who can read the Javascript and tell you what it does or you will need to use one of the other approaches I outlined already, like finding out what the Javascript does and replicating that with Perl or using an implementation of WWW::Mechanize that understands Javascript.

        Just guessing from the content of the data, I would assume that the Javascript simply sends a POST request with the MIME type application/x-www-form-urlencoded and some parameters. If you are wildly guessing, you could try to construct a HTTP::Message that contains that data and see if that already works for you.

        A bit of HTML and HTTP understanding will be necessary though.