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

First, I have to say that despite the fact that my program still doesn't work, I have learnt so much from asking my questions at Perlmonks. Thank you Monks!

OK, so I'm using WWW::Mechanize to try to login to a site so that I can parse some of the content on the site. Unfortunately, it looks like I'm not successfully 'logging in'.

The form on the site looks like this:

<form onSubmit="return validLogin( this );" action="/servlet/LoginServ +let" METHOD="post" name="enterLogin"> <input type="text" tabindex="1" size="8" name="loginId"> <input type="password" tabindex="2" size="8" name="Password"> <input TYPE="hidden" name="action" value="loginFrame"> <input type="hidden" name="app" value=""> <input type="hidden" name="sessType" value="new"> <input type="image" tabindex="3" src="/homepage/gobutton.gif" border=" +0"> </form>

I used the update_html method to remove "onSubmit=return validLogin( this );" so that it doesn't call the javascript (it just changes the username/password to uppercase).

Here is what my code looks like:

use LWP::Simple; use WWW::Mechanize; my $m = WWW::Mechanize->new( autocheck=>1 ); $agent->cookie_jar(HTTP::Cookies->new); # point to the url of the login form $m->get("http://www.mydomain.com/servlet/LoginServlet"); my $html = $m->content; $html =~ s[onSubmit="return validLogin\( this \);"][]g; $m->update_html($html); $m->submit_form( form_name => "enterLogin", fields => { loginId=> "XXXXXXX", Password=> "YYYYYYY", action=> "loginFrame", app=> "", sessType=> "new" } ); print "success?:"; print $m->success(); print "\n"; my $url1 = "http://www.mydomain.com/private/Servlet?var1=apples&var2=o +ranges"; $m->get($url1); print $m->content;
The code above prints the login page ....meaning, it didn't really log me in.

Any ideas why this isn't working? Am I supposed to be doing something with the input/image tag?

thank you!

2006-06-08 Retitled by GrandFather, as per Monastery guidelines
Original title: 'Not logging in'

Replies are listed 'Best First'.
Re: Script using WWW::Mechanize fails to log in
by arkturuz (Curate) on Jun 08, 2006 at 07:50 UTC
    Hi!
    I executed your code *as is* without changes, and I believe that you first must use strict; and then you will find out that you cannot
    my $agent->cookie_jar(HTTP::Cookies->new);
    if you have not initialized user agent like:
    my $agent = LWP::UserAgent->new;
    I hope it's a good start for you. Cheers!
      I initialized the agent variable, but it still doesn't work. I was just playing around and wondering whether cookies were the reason that it wasn't working and accidentally posted it to perlmonks. Anything else that you see that might cause it not to work? Do i have to use cookiejar?
        A reply falls below the community's threshold of quality. You may see it by logging in.
        You need cookies for later authentication, after you succesfully log in. Also, I believe that you must initialize cookies in WWW::Mechanize object.
Re: Script using WWW::Mechanize fails to log in
by wfsp (Abbot) on Jun 08, 2006 at 12:08 UTC
    fwi, this is a simplified version that works ok.

    Form (monk.html)

    <html> <head> <title>test mech</title> </head> <body> <form action="/cgi/process.cgi" METHOD="get" name="enterLogin" > <input type="text" size="8" name="id"> <input type="password" size="8" name="pwd"> <input type="image" src="pic/pic01.jpg" border="+0"> </form> </body> </html>
    The script called by the form (process.cgi)

    #!c:\Perl\bin\perl.exe use warnings; use strict; use CGI::Simple; use CGI::Carp (qw(fatalsToBrowser)); my $q = CGI::Simple->new(); print $q->header; my $pwd = $q->param('pwd'); my $id = $q->param('id'); print " <html> <head> <title>test mech</title> </head> <body> <p>hi there</p> <p>id: $id</p> <p>pwd: $pwd</p> </body> </html> ";
    And the Mech script

    #c:/Perl/bin use warnings; use strict; use WWW::Mechanize; my $m = WWW::Mechanize->new( autocheck=>1 ); my $agent = LWP::UserAgent->new; $agent->cookie_jar(HTTP::Cookies->new); $m->get("http://localhost/monk.html"); my $r = $m->submit_form( fields => { id => "XXXXXXX", pwd => "YYYYYYY", } ); print $m->content;
    Output:

    ---------- Capture Output ---------- > "c:\perl\bin\perl.exe" m.cgi <html> <head> <title>test mech</title> </head> <body> <p>hi there</p> <p>id: XXXXXXX</p> <p>pwd: YYYYYYY</p> </body> </html> > Terminated with exit code 0.
    Having the image field instead of a submit button didn't seem to hurt.

    Hope that helps.

Re: Script using WWW::Mechanize fails to log in
by planetscape (Chancellor) on Jun 09, 2006 at 03:17 UTC

    I would try using a module such as HTTP::Recorder or WWW::Mechanize::Shell to record a successful manual form submission. The output of HTTP::Recorder, for instance, can be "dropped" right into your WWW::Mechanize scripts.

    Another important tool for finding out what is really happening behind the scenes between server and browser is a protocol analyzer such as Ethereal.

    HTH,

    planetscape
Re: Script using WWW::Mechanize fails to log in
by Anonymous Monk on Jun 08, 2006 at 10:53 UTC
    After 'logging in' , I get the following results with frames. Could frames be interfering with everything?
    <html> <head> <title></title> </head> <frameset rows="140,*" border="0" frameborder="no" framespacing="0" bo +rdercolor="black"> <frame SRC="/portal/servlet/LoginServlet?action=blankPortal" name= +"ME_TOP" noresize marginheight="0" marginwidth="0" scrolling="no" fra +meborder="no"> <frame SRC="/portal/servlet/LoginServlet?action=regular&app=&Passw +ord=XXXX&loginId=YYYY&sessType=new" name="ME_BOTTOM" noresize marginh +eight="0" marginwidth="0" frameborder="no"> </frameset> </html>
A reply falls below the community's threshold of quality. You may see it by logging in.