Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

WWW::Mechanize to Fill in Fields when no Form Defined

by egonspengler3 (Initiate)
on Apr 01, 2011 at 20:36 UTC ( #896992=perlquestion: print w/replies, xml ) Need Help??

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

Hello Everyone,

I am trying to use WWW::Mechanize to automatically fill out a web form.

Ordinarily, it is easy because $mech automatically numbers the <form></form> in order when it first gets the $url. But what if there is no <form></form>?

Ordinarily webforms follow this template:

<form> <input id="foo" type="" /> <input id="submit" type="submit" /> </form>

But what if it's only this:

<input id="foo" type="" /> <input id="submit" type="submit" />

  1. How do I fill in fields when they're not enclosed with <form></form>?
  2. Can I refer to those fields by their "id" in $mech because the "name=" attribute is not defined?

Replies are listed 'Best First'.
Re: WWW::Mechanize to Fill in Fields when no Form Defined
by Corion (Pope) on Apr 01, 2011 at 20:38 UTC

      If there is no <form>, then it can't be submitted without Javascript.

      That's technically not correct. It can't (shouldn't) be submitted by a browser, but there is no reason why you can't do it in Perl. You can view what's going on by using wireshark or something similar. Then, just take the response you captured, and communicate to the server directly using IO::Socket::IP. Just replay the communication with your changes.

      If you really want the missing <form> elements, hack your own proxy with HTTP::Proxy.

      If you need to "simulate" a full blown browser session, you could try WWW::Mechanize::Firefox. If you are desperate (or want to have some fun with Pay-per-click advertising), there are also the modules X11::GUITest and Win32::GuiTest.

        There is no need to go down to raw socket communication. After investigating what happens, you can create the appropriate form as HTML::Form object and use WWW::Mechanize to send the data from the form.

Re: WWW::Mechanize to Fill in Fields when no Form Defined
by Kanji (Parson) on Apr 03, 2011 at 03:15 UTC

    If the <form> tags are missing, you can add your own with WWW::Mechanize's update_html method:-

    $ua->update_html('<form>' . $ua->content . '</form>');


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://896992]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2021-12-04 15:55 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (30 votes). Check out past polls.