Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Follow through POST method

by Agyeya (Hermit)
on May 05, 2004 at 07:46 UTC ( [id://350704]=perlquestion: print w/replies, xml ) Need Help??

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

I have a form which uses POST method. I am using WWW::Mechanize.

There are three radio buttons which on clicking take me to a different page but displaying info about a different item. The links are followed through by a javascript. I want to know how to follow through to the next page.

if required i will provide the page address i am trying to access.

Thanx in advance

Replies are listed 'Best First'.
Re: Follow through POST method
by Corion (Patriarch) on May 05, 2004 at 07:52 UTC

    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.
      <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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-26 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found