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

I'm having trouble getting buttons to fire on a web page that has several unamed buttons via Mechanize.

1. I tried mech->submit_form() which failed to click the button but correctly updated the form values. (As expected.)

2. Next I tried following the submit_form() by a click_button(number => 3) and when that failed I tried click_button(value => "Save'n Start". Both of these failed to work.

3. So then I tried individually setting the form values via form_number() and 3 field(name,val) follwed by the two click_button calls. Again, no luck and this time the form values were not updated either.

Any ideas?
#!/usr/bin/perl use WWW::Mechanize; ## Automate Grabbing Webpages use strict; ## What URL shall we retrieve? my $url = "http://10.10.10.10/bdtconfig.shtml"; my $mech = WWW::Mechanize->new(autocheck => 1); $mech->default_headers->authorization_basic("user", "pass"); $mech->get($url); my $page = $mech->content; $mech->dump_all(undef, 1); #print $page; my $resp2=$mech->submit_form( form_number => 1, fields => { bdtminvolt => 49.0, bdtmaxtime => 0.3, rect_stby_volt => 45.0 ,}, ); print "Now clicking ...\n"; my $resp3=$mech->click_button(value => "Save'n Start"); #$mech->form_number(1); #$mech->field('bdtminvolt',48.0); #$mech->field('bdtmaxtime',0.2); #$mech->field('rect_stby_volt',45.0); #my $resp3=$mech->click_button(number => 1); #print $resp3->content;
Here is the output, which include a dump_all().
POST http://10.10.3.223/bdtcfg_gen.shtml [bdtForm] <NONAME>=Clear Alarm... (button disabled) <NONAME>=Clear Results... (button) low_volt=52.0 (text) bdtminvolt=49.0 (text) bdtmaxtime=0.2 (text) rect_stby_volt=45.0 (text) very_low_volt=48.0 (text) bdtcycle=13 (text) <NONAME>=3 (option) [*3/Observation|4/Minor|5/ +Major|6/Cr itical] <NONAME>=2 (option) [0/None|1/On Raise|*2/On R + & C|3/On Cease] <NONAME>=2 (option) [0/None|1/On Raise|*2/On R + & C|3/On Cease] <NONAME>=Save No Start (submit) <NONAME>=Save'n Start (submit) <NONAME>=Stop Test (submit) <NONAME>=<UNDEF> (reset) <NONAME>=Factory Default... (button) bdt_event_id=0 (hidden readonly) eid56=117 (hidden readonly) Now clicking ...

Replies are listed 'Best First'.
Re: Multiple NONAME buttons and WWW::Mechanize
by almut (Canon) on Feb 06, 2010 at 00:36 UTC

    Could you show the HTML source of the respective page?  Is there any Javascript involved?

      Sorry about that. Here's the page.

      <html> <head> <link rel="stylesheet" type="text/css" href="libcss.css"> <script src="jslib.js" type="text/javascript"></script> <script language="javascript" type="text/javascript"> function CA() { switch(0) { case 0: var msg="C B alarm?"; break; case 1: var msg="Aas de la B?"; break; } if(confirm(msg)) location.href='BAC'; } function CResults() { switch(0) { case 0: var msg="C B r?"; break; case 1: var msg="B R B?"; break; } if(confirm(msg)) location.href='BResultsC'; } function checkfields() { var msg0; var msg1; var msg2; var msg3; switch(0) { case 0: msg0="T A V lt Bus V!"; msg1="T A V gt R St V!"; msg2="T D gt 0."; msg3="R St V gt LVD!"; break; case 1: msg0="E V de Bus actual!"; msg1="El V de V del R!"; msg2="La d 0."; msg3="El v e"; break; } if (4!=2) { if(parseFloat(document.bForm.rect_stby_volt.value)<=42.0) { alert(msg3); document.bForm.rect_stby_volt.focus(); return false; } if(parseFloat(document.bForm.bminvolt.value)<=parseFloat(document.b +Form.rect_stby_volt.value)) { alert(msg1); document.bForm.bminvolt.focus(); return false; } } if( parseFloat(document.bForm.bminvolt.value) >= "54.5") { alert(msg0) document.bForm.bminvolt.focus(); return false; } if (parseFloat(document.bForm.bmaxtime.value) <= 0) { alert(msg2); document.bForm.bmaxtime.focus(); return false; } return true; } function BSubmit() { if (checkfields()==false) return false; handle=EventHandlingNum(document.getElementById('level_99').value, +document.getElementById('trap_99').value,document.getElementById('mai +l_99').value,1); document.bForm.TAG99.value=handle; // document.bForm.submit(); } function onLoadB(rate) { // if(document.getElementById('runbutton').disabled)//b running,kee +p refreshing var disable_str=""; // if(disable_str.indexOf('disable')>=0)//B running if(0==2)//brunning { document.getElementById('stopbutton').disabled=false; // document.getElementById('level_99').disabled=true; // document.getElementById('mail_99').disabled=true; // document.getElementById('trap_99').disabled=true; window.setTimeout("location.href=document.location.href",rate); } else { document.getElementById('stopbutton').disabled=true; } } </script> </head> <body onLoad="onLoadB(5000); parent.OnLoadFunc()" onUnload="parent.OnU +nloadFunc()"> <form name="bForm" ACTION="/bcfg_gen.shtml" METHOD="POST" onSubmit="re +turn BSubmit()"> <TABLE align="center" BORDER=0 CELLSPACING=1 CELLPADDING=5> <TR> <td bgcolor="white">&nbsp;</td> <!--BT--> <TD colspan=4 align=center bgcolor=#e0e0e0> <b>BT</b></TD> </TR> <tr bgcolor="#e0e0e0"> <td bgcolor="white">&nbsp;</td> <!--Last E--> <td align="left">Last E</td> <td colspan="3" nowrap="nowrap" align="center">2010-02-05 16 +:44:57</td> </tr> <tr bgcolor="#e0e0e0"> <td bgcolor="white">&nbsp;</td> <td>S/E/T</td><td colspan="2"> 54.5 V/ 49.8 V/ 0.02 Hrs </td> <td align="center" bgcolor="#e0e0e0">U</td> </tr> <!--AC S--> <tr><td></td><td><b><i>AC S/Mode:</td><td align=center style="bord +er:double" bgcolor="#00ff00"><b>F</b></td> <td><input type="button" disabled value="C A..." onclick="CA()"> +</td> <tr bgcolor="#e0e0e0"> <td><input type="button" value="C R" onclick="CResults()"></td> </tr> <tr bgcolor="#e0e0e0"><td bgcolor="white"></td><td>B T R</td><td a +lign="center" colspan="3" >(NC)</td></tr> <!--bat T r--> <tr bgcolor="#e0e0e0"><td bgcolor="white">&nbsp;</td><td>Sys DC Bu +s V</td><td align="right">54.5</td> <td nowrap>V</td><td align=center + bgcolor="#00ff00"> No</td></tr> <!-- sys v --> <tr bgcolor="#e0e0e0"><td bgcolor="white"></td><td>B AC</td><td al +ign="center" colspan="3" >(NSC)</td></tr> <!-- bat current --> <tr bgcolor="#e0e0e0"><td bgcolor="white">&nbsp;</td><td>B MP V (1 +.3*2)</td><td align="right">2.6</td> <td>V</td><td align=center bgcol +or="#00ff00"> No</td></tr> <!-- bat mP --> <tr bgcolor="#FFCC00"> <td bgcolor="white">&nbsp;</td> <td>Low Sys V A</td> <td align="right"> <input name="low_volt" type="text" style="text-align: right; fon +t-weight:bold;" value="52.0" size="5" maxlength="5" /> </td> <td>V</td> <td>&nbsp;</td> </tr> <tr><td></td><td colspan="4"><b><i>T Param:</td></tr> <tr bgcolor="#e0e0e0"> <td bgcolor="white">&nbsp;</td> <td>T A V </td> <tr><td></td><td colspan="4"><b><i>T Param:</td></tr> <td align="right"> <input name="bminvolt" id="bminvolt" type="text" style="text +-align: right; font-weight:bold; " value="49.0" size="5" maxlength="5 +" /> </td> <td>V</td> <td>&nbsp;</td> </tr> <tr bgcolor="#e0e0e0"> <td bgcolor="white">&nbsp;</td> <td>T D</td> <td align="right"> <input name="bmaxtime" type="text" style="text-align: right; + font-weight:bold;" value="0.3" size="5" maxlength="5" /> </td> <td>Hrs</td> <td>&nbsp;</td> </tr> <script language="javascript" type="text/javascript"> if(0==2) document.write("<tr bgcolor=#e0e0e0 style=visibility:hidden>"); else document.write("<tr bgcolor=#e0e0e0>"); </script> <td bgcolor="white">&nbsp;</td> <td>R St V</td> <td align="right"> <input name="rect_stby_volt" type="text" style="text-align: righ +t; font-weight:bold; " value="45.0" size="5" maxlength="5" /> </td> <td>V</td> <td>&nbsp;</td> <td bgcolor="white">&nbsp;</td> </tr> <tr><td></td><td colspan="4">L B N</td></tr> <tr bgcolor="#FF0000"> <td bgcolor="white">&nbsp;</td> <td>VLSVA</td> <td align="right"> <input name="very_low_volt" type="text" style="text-align: right +; font-weight:bold;" value="48.0" size="5" maxlength="5" /> </td> <td>V</td> <td>&nbsp;</td> </tr> <tr bgcolor="#e0e0e0"> <td bgcolor="white">&nbsp;</td> <td>E LVD </td> <td align="right"> 42.0 </td> <td>V</td> <td>&nbsp;</td> </tr> <tr><td></td><td colspan="4"><b><i>A E of T:</i></b></td></tr> <tr bgcolor="#e0e0e0"> <td bgcolor="white">&nbsp;</td> <td>C30</td> <td align="right"> <input name="bcycle" type="text" style="text-align: right; f +ont-weight:bold; " value="13" size="4" maxlength="4" /> </td> <td colspan="2">0 D</td> </tr> <tr bgcolor="#e0e0e0"> <td bgcolor="white">&nbsp;</td> <td align="left">Next E</td> <td colspan="3" nowrap="nowrap" align="center">2011-03-02 17:06: +59</td> </tr> <tr> <td></td> <td colspan="4"><b><i>EH:</i></b></td> </tr> <tr> <td></td> <td colspan="4"> <table width="100%" cellpadding="0" cellspacing="0"> <tr> <TD></TD> <td>B F</td><td>No</td><td>Yes</td> <td></td> <td><select id="level_99"><option value=3 SELECTED>Obs<option value=4 +>Min<option value=5>Maj<option value=6>Crit</select></td> <td><select id="trap_99"><option value=0>None<option value=1>On Raise +<option value=2 SELECTED>On R &amp; C<option value=3>On Cease</select +></td> <td><select id="mail_99"><option value=0>None<option value=1>On Raise +<option value=2 SELECTED>On R &amp; C<option value=3>On Cease</select +></td> </tr> </table> <tr> <td></td> <td colspan="4"> <div align="center" > <input type="submit" value="Save No Start" onClick="document. +bForm.b_event_id.value=0" /> <input type="submit" id="runbutton" value="Save'n Start" onCl +ick="document.bForm.b_event_id.value=1;" /> <input type="submit" id="stopbutton" value="Stop" onClick="doc +ument.bForm.b_event_id.value=2"/> <input type="reset" value="Reset" /> <INPUT TYPE="BUTTON" VALUE="FD" onClick="ConfirmPersReset('BR +eset','',0)"> </div> </td> </tr> </table> <input name="b_event_id" type="hidden" value="0" /> <input type="hidden" name="TAG99" value="117"> </form> </body> </HTML>
        You're setting fields that don't exist - rename 'bdtminvolt' to 'bminvolt' and 'bdtmaxtime' to 'bmaxtime' and see what happens.
      As you can see there is JAVASCRIPT code. Could that screw up the parsing? Should I use LWP module instead or do a one off hack?
        I see that Mechanize doesn't handle java script and the buttons invoke java script. I'll see if I can preprocess within PERL and hack around to invoke the button.