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

Hi,


I am trying to access a web page using WWW::Mechanize and i come across a situation where i need to click on a button with no name. I tried button_click option of WWW::Mechanize with both value and number parameter but the program throws an error in both the cases.


THE HTML CODE:

<input type="button" value="Login" class="inputs2" onClick="lookup();">


I looked up the JavaScript Code but could not understand head-or-tail from it. Below is the corresponding JavaScript code.
<script type="text/javascript"> function lookup() { userO=document.getElementById("loginname"); passO=document.getElementById("loginpass"); user=userO.value; pass=passO.value; if(user.length == 0 || pass.length==0) { $('#box').hide(); } else { $('#box').show(); $('#serverlisted').html('Loading Connection Routine'); mns=document.getElementById("mns"); ck=mns.checked; if (ck==true){ $.post("rpc.f", {credenU: ""+user+"", credenP: ""+pass+"", +mns: 1}, function(data){ if(data.length >0) { $('#serverlisted').html(data); } }); } else{ $.post("rpc.f", {credenU: ""+user+"", credenP: ""+pass+""} +, function(data){ if(data.length >0) { $('#serverlisted').html(data); } }); } } } // lookup </script>
In the above code,
loginname = Name of the text box for user name
loginpass = Name of the text box for password
mns = A check box to preserve our login for 10 hours
Could any of the monks please help me in resolving this issue?


Thanks

Replies are listed 'Best First'.
Re: Accessing unnamed button using WWW::Mechanize
by bichonfrise74 (Vicar) on Sep 10, 2009 at 23:06 UTC
    If there is only one button in the form, then you can just specify something like...
    $mech->click();
    That should do it for you.
      onclick of the button calls a javascript function, therefore I don't think just clicking the button through Mechanize will serve the purpose (unless any other module for javascript such as WWW::Mechanize::Plugin::JavaScript is used.)
      If you look at the javascript code
      if (ck==true){ $.post("rpc.f", {credenU: ""+user+"", credenP: ""+pass+"", mns: 1} +, function(data){ if(data.length >0) { $('#serverlisted').html(data); } }); } else{ $.post("rpc.f", {credenU: ""+user+"", credenP: ""+pass+""}, functi +on(data){ if(data.length >0) { $('#serverlisted').html(data); } });
      it seems that JSON data is posted to url "rpc.f" and the data returned is added to #serverlisted (should be an id to div, span or any other tag on page.)
      In my view you need to post the data
      which is either {credenU: ""+user+"", credenP: ""+pass+"", mns: 1} or {credenU: ""+user+"", credenP: ""+pass+""} based on value of mms
      to url "rpc.f" and replace the response recived on tag #serverlisted
      Update: Corrected the typo. thanks marto for pointing it.
      Regards,
      Ashish
      There are two buttons in the form
Re: Accessing unnamed button using WWW::Mechanize
by Anonymous Monk on Sep 11, 2009 at 10:59 UTC