The following is an attempt to use LWP to populate a form. I want to fill out the form, press submit, and see the result.

Update: Turns out I was not properly submitting the form with HTML::Form. See Corion's reply for details.

When I submit this form, the result I get back in my UserAgent is not what I expect. Instead of a page showing the results based on my form fields, what I get back is the default page again. Because there Javascript embedded into the page I am browsing in my script, which HTML::Form cannot interact with, I tried emulating the Javascript in my code.

Any warnings you see if you try to run the code are harmless per the HTML::Form docs. Naturally a hidden field set to read-only will give a warning when it is populated.

Can anyone see how I need to setup this form for the server to accept my post? Where are some likely places that might have issues in the code?

Here's the code:

#!/usr/bin/perl # browse.pl use strict; use warnings; use CGI ':standard'; use LWP::UserAgent; use HTML::Form; use Data::Dumper; my $browser = LWP::UserAgent->new; my $browse_url = 'http://browseusers.myspace.com/Browse/Browse.aspx'; my $response = $browser->get($browse_url); my @forms = HTML::Form->parse($response); # Pull ACTION out of JavaScript function, replace in FORM element my $content = $response->content; $content =~ m{document\.frmBrowse\.action = "(.*?)"}; my $action_url = "http://browseusers.myspace.com/Browse/" . "$1"; $forms[1]->action($action_url); my $action = $forms[1]->action; # Get Form Elements my $zipRadius = $forms[1]->find_input("zipRadius", "option"); my $zipCode = $forms[1]->find_input("zipCode", "text"); my $Scope = $forms[1]->find_input("Scope", "radio"); # Get Hidden Elements my $__EVENTTARGET = $forms[1]->find_input("__EVENTTARGET"); my $Page = $forms[1]->find_input("Page"); # Assign Values $zipRadius->value("5"); $zipCode->value("92630"); #$Scope->value("scopeMyFriends"); #Populate Hidden Elements (WARNINGS OK) $__EVENTTARGET->value("update"); $Page->value("1"); # Update Form $forms[1]->click("update"); # Get Response from Server $content = $response->content; print $content; # Dump Form (For Testing Only) #print $forms[1]->dump;

In reply to HTML::Form Submit Issue by initself

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.