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

HI I am trying to make my script login to a web server and then go to the posting page I want and automate a posting there. I have been looking over this thing forever it logs in and navigates however it will not produce the POST result and I cannot see it on the board after I run the script. Even though the response returns an OK. So I am not sure what to do. I am stuck :( Any help would be great
use HTTP::Cookies; use WWW::Mechanize; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); my $mech = WWW::Mechanize->new(); $mech->cookie_jar(HTTP::Cookies->new()); my $url = "****"; my $url2 = "****; my $username = "***"; my $password = "*****"; $subject = "dhdfghdfghfgh"; $message = "blah"; $mech->get($url); #my @names = $mech->forms(); $mech->form_number(2); $mech->field(username => $username); $mech->field(password => $password); $mech->click(); $mech->success or die "post failed: ", print $mech->response->status_line; $mech->get($url2); #my @names = $mech->forms(); $mech->form_number(2); $mech->field(subject => $subject); $mech->field(message => $message); $mech->click(); $mech->success or die "post failed: ", print $mech->response->status_line;

Replies are listed 'Best First'.
Re: Form won't post
by missingthepoint (Friar) on Apr 06, 2009 at 08:50 UTC

    I suspect it isn't working because you're $mech->geting $url2. I don't think you want to do that - after you've POSTed with $mech->click, $mech's "current url" ($mech->uri) will change. Try removing the line $mech->get($url2); and see if it works.

    I can see a few other problems with your code:

    • You don't need to use LWP::UserAgent; WWW::Mechanize is a subclass of it which means it use it for you.
    • Don't think you need to use HTTP::Cookies either; I can do $mech->cookie_jar(HTTP::Cookies->new()); only using WWW::Mechanize.

    undef $lunches{free}
Re: Form won't post
by Gangabass (Vicar) on Apr 06, 2009 at 08:59 UTC

    I think you need to check $mech->content() after first submit. May be you did't pass autorization. For some sites you need to use button argument, and some sites require to send also x and y position of the mouse.

    Also some site may use JavaScript. So please check if your target site works with JavaScript disabled.