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

Hi guys, I am very new to Perl and i am learning on the fly while i try to automate some projects for work. So far its has been a lot of fun.

I am working on generating a report for a customer. I can get this report from a web page i can access.
First i will need to fill a form with my user name, password and choose a server from a drop down list, and log in.
Second i need to click a link for the report section.
Third a need to fill a form to create the report.

I found that its hard for me to debug what i do since i cant see the webpage i currently hold in $mech and verify i am on the right place.
Second, i am not sure i read the source code correctly to find the field names etc.

Here is what i wrote so far:

my $mech = WWW::Mechanize->new(); my $url = 'http://xxx; $mech->get( $url ); $mech->submit_form( form_number => 1, fields => { Username => 'someone', Password => 'pass', Server => 'Live' }, ); die unless ($mech->success);

The source code is:

<!-- login panel begin --> <div id="ctl00_ctl00_cphserver_cphLogin_pnlLogin" class="leftpanel" st +yle="Visibility:visible;Display:block;"> <span style="display:inline-block;width:120px;">Username</span> <br \> <input name="ctl00$ctl00$cphserver$cphLogin$txtUser" type="text" id="c +tl00_ctl00_cphserver_cphLogin_txtUser" onKeyPress="javascript: return + txtUser_onKeyPress();" style="color:Navy;width:140px;" /> <br \> <span style="display:inline-block;width:120px;">Password</span> <br \> <input name="ctl00$ctl00$cphserver$cphLogin$txtPW" type="password" id= +"ctl00_ctl00_cphserver_cphLogin_txtPW" onKeyPress="javascript: return + txtPW_onKeyPress();" style="color:Navy;width:140px;" /> <br \> <span style="display:inline-block;width:120px;">Server</span> <br \> <select name="ctl00$ctl00$cphserver$cphLogin$ddlServers" id="ctl00_ctl +00_cphserver_cphLogin_ddlServers" style="color:Navy;width:140px;"> <option value="Live">Live</option> <option selected="selected" value="Test">Test</option> </select> <br \> <br \> <input type="submit" name="ctl00$ctl00$cphserver$cphLogin$btnSignIn" v +alue="Sign-In" id="ctl00_ctl00_cphserver_cphLogin_btnSignIn" /> <br /> <br /> </div> <!-- login panel end -->

Please help me the understand how to debug easily and how to read the source code so i can program correctly.

Thank you! Yaniv

Replies are listed 'Best First'.
Re: using and debuging www Mechanize
by Corion (Patriarch) on Jun 08, 2009 at 12:32 UTC

    Hello and welcome!

    If you want to see "where" you are with a WWW::Mechanize object, you can use HTML::Display to display the current page in a browser, or, and this is most likely easier in your situation, just use

    $mech->dump_forms();

    to get a feel for the names of the fields on the forms. You will also in the long run need to learn some HTML. The name for the "username" field is ctl00$ctl00$cphserver$cphLogin$txtUser, and you'll find the name for the "password" field quier easily from that.

      Thanx! So if my code looks like this
      $mech->submit_form( form_number => 1, fields => { 'ctl00$ctl00$cphVeriCentre$cphLogin$txtUser' => 'someone', 'ctl00$ctl00$cphVeriCentre$cphLogin$txtPW' => 'pass', 'ctl00$ctl00$cphVeriCentre$cphLogin$ddlServers' => +'Live', }, ); die unless ($mech->success); $mech->dump_forms();
      I should see the new page i got directed to after submitting the form?
      Because i don't.
      I also noticed there are two type="submit" parts in the source code, the second one is in another panel, can that cause a problem?

      Thank you

        HTML does not have "panel"s, so it's kinda hard to tell what you mean by this. If you're not ending up at the page you expect to, you'll need to debug that issue, possibly by looking at whether the HTML contains some error message or something.

        Mechanize will use the "submit" button associated with your form. Maybe take another look at what ->dump_forms() outputs.

Re: using and debuging www Mechanize
by lakshmananindia (Chaplain) on Jun 08, 2009 at 12:34 UTC

    You can give the name of the text boxes instead of

      Username    => 'someone',Password    => 'pass'

    For example if you have a text box like

    <input id="USER_NAME" type="text" name="USER_NAME"/>

    then you can to submit as follows

    my $response=$mech->post($url,[ 'USER_NAME' => "testing"]); | \ / name of the text box

    Update: Changed "have and need" to can

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


      You have to give the name of the text box.

      No, form_number is perfectly acceptable (see WWW::Mechanize).

      then you need to submit as follows

      Need? No. You may use post , or simply continue using submit_form.

Re: using and debuging www Mechanize
by Anonymous Monk on Jun 08, 2009 at 12:56 UTC
    You should check to see if you can do it using the cPanel Xml API first, it should be easier.