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

Greetings Monks, I come to you today in search of knowledge. Here is my question. I am trying to get Perl to fill in some login information on a website and then submit it and get the response back. (For those who love visual aids)
User Name: Username <---- I would like Perl Password : ******** <---- to fill these in. _______ | Login | <---- click with mouse normally -------
It would be a great help someone could explain or show me how to do this. Thank you for your time, Rax

Replies are listed 'Best First'.
Re: Filling in and Submitting a Form/Login Info
by ikegami (Patriarch) on Aug 21, 2009 at 21:16 UTC
    #!/usr/bin/perl use strict; use warnings; use utf8; use open ':std' => ':utf8'; use CGI qw( -utf8 ); my $cgi->new(); print($cgi->header('text/html; charset=utf-8')); print <<'__EOI__'; <title>Demo</title> <form action="POST"> <p>User Name: <input type="text" value="Username"> <br>Password: <input type="text" value="********"> <br><input type="submit" value="Login"> </form> __EOI__
Re: Filling in and Submitting a Form/Login Info
by Marshall (Canon) on Aug 21, 2009 at 22:04 UTC
    This is an LWP program.

    Basically what you need to do is to "fake the website out" into thinking that you are a normal browser.

    There is more than one way to do this.

    -You normally will need Crypt::SSLeay as usually these log-in pages are https (secure encrypted things).
    -Sometimes you need a Cookie Jar, below this is a temporary thing.

    Look at Mechanize as that is popular for this sort of stuff.

    But the general idea is that the website thinks that you are Firefox 3.0, IE version whatever and you ask for a log-in page. Below I claim to be an ancient browser because the server sends me different stuff depending upon who I claim to be. If I claim to be IE 6, then I get different stuff and different responses are required from me. With IE a major thing is the "view state". Anyway basically claim to be as stupid a thing as you can get away with.

    There are 2 ways to send stuff to a web server, one like this: http://www.perlmonks.org/?parent=790461;node_id=3333 which encodes what you want into the URL and "Post" which is a different way. A secure log-in will normally require POST.

    Below I just copied the preamble to a LWP program that runs every day for me. It does a secure log-on to a server and then does some stuff that isn't relevant here.

    In the case of this log-in form, 'user' => 'username', 'pwd' => "password" aren't really what these things are called in the actual HTML form that is received and there are a couple of other fields that I have to supply. Normally I have to experiment and look at the HTML code that is coming to me to get the form name right, etc.

Re: Filling in and Submitting a Form/Login Info
by leocharre (Priest) on Aug 21, 2009 at 21:03 UTC

    Why oh why are you trying to do this?

    Why would you use an API to mingle with a GUI to then mess with an API again?????

    Look, whatever site this is.. you should be able to use some perl API, for example .. . You wouldn't use http://search.cpan.org to get info about perl modules within a script would you? No, you would use CLI cpan or someting..

    Whatever site you're thinking of, it may have a API for you right on cpan right the muck now.. Check out facebook, myspace... I think there's even a gmail, yahoo.. etc..

    What I am trying to suggest to you, is that the login forms on the browser (the GUI (graphical user interface)) are just for human beings. And there may be something out there so you don't have to do this.. Which.. if you did.. would be through some WWW::Mechanize process... you can search for this on perlmonks. The best way is to go to google and search for: site:perlmonks.org WWW::Mechanize login

Re: Filling in and Submitting a Form/Login Info
by abijr (Novice) on Aug 21, 2009 at 21:24 UTC
    There is a module on CPAN that you should check out, maybe it will be useful. Its called: WWW::Mechanize http://search.cpan.org/~petdance/WWW-Mechanize-1.60/lib/WWW/Mechanize.pm

      I agree... I use WWW::Mechanize all the time for this. You can also use LWP or something to have it manage cookies. Here is a quick code sample for you, to get started:

      use strict; use warnings; use WWW::Mechanize; use HTTP::Cookies; my $mech = WWW::Mechanize->new( agent =>$browser_agent); $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url_login); $mech->form_number($form_number); $mech->field('username' => $username); $mech->field('password' => $password); #Submit the form.. ahoy ahoy $mech->click();

      Just use Firefox addon Web Developer to lookup the form details with ease... thats my personal preference at least.

      Sorry, didn't read all of leocharre's post.