in reply to Follow through POST method

If you have JavaScript that submits the forms, you have the choice of two ways with WWW::Mechanize:

  1. (easy) Look at the JavaScript code and reprogram that functionality in the Perl code.
  2. (easy to hard) Use JavaScript::Spidermonkey to embed a JavaScript engine in your Perl program and run the Javascript code through that. You will have to supply appropriate wrappers so the browser DOM will be reflected in your WWW::Mechanize script. This can be very hard. See the first option for an alternative.

Replies are listed 'Best First'.
Re: Re: Follow through POST method
by Agyeya (Hermit) on May 05, 2004 at 10:05 UTC
    <form name="frmChooseZone" method="post" action="ChooseZone.aspx?type= +A" id="frmChooseZone"> <input type="hidden" name="__EVENTTARGET" value="" /> <input type="hidden" name="__EVENTARGUMENT" value="" /> <input type="hidden" name="__VIEWSTATE" value="dDw0NzUyOTE0NzQ7" /> <script language="javascript"> <!-- function __doPostBack(eventTarget, eventArgument) { var theform; if (window.navigator.appName.toLowerCase().indexOf("netscape") + > -1) { theform = document.forms["frmChooseZone"]; } else { theform = document.frmChooseZone; } theform.__EVENTTARGET.value = eventTarget.split("$").join(":") +; theform.__EVENTARGUMENT.value = eventArgument; theform.submit(); } // --> </script> Please Select the Consular District that covers your Residence TT Services - Web Application <table id="rdlZone" class="RBtnList" cellspacing="1" cellpadding="1" b +order="0"> <input onclick="__doPostBack('rdlZone_0','')">US Consulate Chennai (Ma +dras) <input onclick="__doPostBack('rdlZone_1','')">US Embassy New Delhi <input onclick="__doPostBack('rdlZone_2','')">US Consulate Calcutta (K +olkata)
    This is the part of the relevant code of the webpage.

    Now how do i goto each of these pages in order. What do I append with the address of this page to goto the three choices.

      I am not sure how you come to the conclusion that you should have to append anything to the address of the page. My points 1. and 2. do not say anything about appending anything to any address of any page anywhere.

      Since I am lazy, I will show you how I attack the problem using the method described first, the easy translation of the Javascript code to Perl.

      Looking at the definition of the JavaScript __doPostBack routine, it mostly replaces ever $ by a : and then sets the fields __EVENTTARGET and __EVENTARGUMENT in the form to the passed values. So, to simulate a "click" on one of the region selections, one would have to set these fields correctly, too.

      use strict; use WWW::Mechanize; my $agent = WWW::Mechanize->new( autocheck => 1 ); sub __doPostBack { my ($eventTarget,$eventArgument) = @_; $agent->current_form->field('__EVENTTARGET',$eventTarget); $agent->current_form->field('__EVENTARGUMENT',$eventArgument); }; # ... load the page, and select the various zones

      I will not write the whole script for you. You now have to look through the input fields on $agent->current_form and look which zones are suitable for you and loop over these. You should understand how JavaScript works and how to analyze the code.

        Thanks for the prompt reply!!
        But things here may be simple for you, not for me. Not Complaining. I very much appreciate your help.

        The three regions in the code are actually three radio buttons. If either one is clicked it takes me to a new page. So how do i choose and click the radio button. The process for clicking a button is agent->click(); but what about the radio button. I read the www::mechanize page, but it is still not very clear to me.

        The code for the radio buttons is

        <table id="rdlZone" class="RBtnList" cellspacing="1" cellpadding="1" b +order="0"> <tr> <input id="rdlZone_0" type="radio" name="rdlZone" value="1" onclic +k="__doPostBack('rdlZone_0','')" language="javascript" /><label for=" +rdlZone_0">Chennai Madras</label> </tr> <tr> <td><input id="rdlZone_1" type="radio" name="rdlZone" value="2" on +click="__doPostBack('rdlZone_1','')" language="javascript" /><label f +or="rdlZone_1"></td>US Embassy New Delhi</label></td> </tr> <tr> <td><input id="rdlZone_2" type="radio" name="rdlZone" value="3" on +click="__doPostBack('rdlZone_2','')" language="javascript" /><label f +or="rdlZone_2"></td>Calcutta (Kolkata)</label></td> </tr> </table>