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

Hi, I'm new to Perl. Please take it easy on me.

I want to automatically login and download a Document from a university Platform, using WWW::Mechanize. The login seems to work, but after that I am not able to call any methods on my mechanize Object (I test this with ->title). I know that the site directly redirects after the login. Is this why the object is "broken"? Do I have to somehow deal with the redirect?

#!/usr/bin/perl use warnings; use strict; use LWP::Simple; use WWW::Mechanize; my $derp = WWW::Mechanize->new(); my $url = "myurl"; $derp -> cookie_jar(HTTP::Cookies->new()); $derp -> get($url); $derp -> content(); $derp -> follow_link(text=>"loginLink"); print $derp -> title(), "\n"; my @forms = $derp -> forms(); foreach (@forms) { my @input_fields = $_->param; print "form params are:\n"; foreach (@input_fields) { print "$_\n"; } } print "filling in form..\n"; $derp -> field("j_username", "user"); $derp -> field("j_password", "password"); $derp -> submit(); if($derp -> success()) { print "success\n"; } else { print "something went wrong\n"; } print $derp -> title(), "\n";

The output is success, but I get the following error: Use of uninitialized value in print at messing_around.plx line 43. I think that submit() returns something crap. Is this because I made a mistake or because of the redirect I should somehow deal with?

Replies are listed 'Best First'.
Re: Mechanize login, but redirect.
by Anonymous Monk on May 16, 2016 at 01:15 UTC

    The output is success

    success just means you requested nicely and the server understood, now read the contents of the response to find out what it says  $mech->dump;

     $derp -> cookie_jar(HTTP::Cookies->new());

    redundant, Mech does that already

      Now this is Interesting! print $derp -> dump_text(), "\n"; says:

      Note: Since your browser does not support JavaScript, you must press the Continue button once to proceed.

      I have just confirmed with noScript, that this is the Page I never see, when it redirects me. The "Continue Button" is defined as: <input type="submit" value="Continue"></input> This is great! The login works and I just have to click this damn button. Thank you!

      However... I have trouble clicking this button with $derp ->click(name => "Continue". I have also tried it with "submit". Is a button always part of a Form? Do I have to select a Form first?

        I figured it out. If anybody else has this issue. The button doesn't have a name tag, but a value tag. Therefore you can use $derp -> click_button(value => "value-name");

        Thanks everybody. Is there a way to mark this as solved? I'm new to the Forum.

        Mechanizes uses HTML::Form to process forms, which ignores form elements outside of forms

        if that page is using buttons outside of forms, this means javascript is involved, so solution would be as mech faq says, to discover the url using firefox+ livehttpheaders/wireshark or by parsing the javascript in your program

Re: Mechanize login, but redirect.
by Marshall (Canon) on May 16, 2016 at 01:07 UTC
    Its been awhile since I did any LWP work. However my first thought is that you are doing a login after which I assume you get to an HTTPS site. In that case, you need to have the Crypt::SSLeay Module installed to use the https protocol. I hope I'm not sending you on a wild goose chase, but I remember fighting with this a bit to get https working.

      Thank you for sharing some Input. I have installed the Crypt::SSLeay Module and activated(?) it with use Net::SSL;, but now it can't even fetch the first url, which was, by the way, already an https site.

      Error as follows: Can't connect to www.url.com:443 (Crypt-SSLeay can't verify hostnames) at messing_around.plx line 12. Line 12 is $derp -> get($url);

      Was it already using some other https module by default?