in reply to Re^7: Not able to select the form with WWW::Mechanize::Firefox using form_with_fields()
in thread Not able to select the form with WWW::Mechanize::Firefox using form_with_fields()

When trying to select form with form_number 2, the error message is "No elements found for form number 2".

Both these error messages including "4 elements found for form number 1" leads to quit the program based on the value of the parameter autodie which is set to 1 by default.

Even if I turned off autodie, the value function does not return the present value when the error is "4 elements found for form number 1". Please see below for the code snippet and output for the same:

my $input_name = 'DMPName'; my $web_url = 'http://xxx.xxx.xx.xx/config.asp'; my $mech = WWW::Mechanize::Firefox->new(activate => 0, autoclose => 1, autodie =>0); $mech->get($web_url, no_cache => 1); $current_form = $mech->form_number(1); print "Present value of $input_name: " . $mech->value($input_name) . " +\n";
Output:
4 elements found for form number 1 at ..... No elements found for input with name 'DMPName' at .... Present value of DMPName:
But interestingly while selected the form 10 which is not existing, I got following output:
$current_form = $mech->form_number(10);
No elements found for form number 10 at ... Present value of DMPName: Current Name
  • Comment on Re^8: Not able to select the form with WWW::Mechanize::Firefox using form_with_fields()
  • Select or Download Code

Replies are listed 'Best First'.
Re^9: Not able to select the form with WWW::Mechanize::Firefox using form_with_fields()
by Corion (Patriarch) on Jun 28, 2011 at 16:08 UTC

    As another way of debugging what form gets selected via ->form_number, you can print its HTML content:

    print $mech->current_form->{innerHTML};

    Maybe there is a bug in how the current form gets set up, but without seeing the exact HTML you're talking about (or better, a 10 line example that reproduces the same problem), it's hard to diagnose.

      Thanks for the suggestion.

      I tried to print the current form after selecting it with the form number. Please see the output for the same.

      $current_form = $mech->form_number(10); print "Present value of $input_name: " . $mech->value('DMPName') . "\n +"; print $mech->current_form->{innerHTML}; No elements found for form number 10 at D:\.... Present value of DMPName: Current Name Can't use an undefined value as a HASH reference at D:\...

      But by selecting a form other than form number 1 lets me to access elements of every forms in the webpage including the first form.

      Please find the stripped down version of the HTML.

      <html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>DMP configration</title> <meta http-equiv="pragma" content="nocache" /> <link href="../ormats.css" type="text/css" rel="stylesheet"/> <script src="../config.js" type="text/javascript"> </script> <style type="text/css"> .styleColorSpan {color: #CCCCCC} .stylePositionTable {position:relative;top:5px;} </style> </head> <!-- onLoad( ) --> <!-- --> <body class="scrollbar_black" bgColor=#000000 onload="onLoad( )"> <table class="toptable" cellpadding="1" cellspacing="2" height="100%" +width="100%" border="0"> <!-- <table class="toptable" cellpadding="1" width="100%" height=" +100%" border="0"> --> <tr height="100%"> <td valign="top"> <table width="100%" align=top bgColor=#ffffff vAlign=top> <tr width="100%" cellspacing="0" cellpadding="0"> <form action="/goform/formHandlerConfigureDMPName" method="pos +t" onsubmit = "return validateDMPNameOnSubmit(this)"> <td width="5%"><img src="../invis.gif" alt="" width="50" heigh +t="30"/></td> <td width="45%">Digital Media Player Name:</td> <td width="50%"><input type="text" name="DMPName" value="mmmmm +mmm" maxlength="129"/>&nbsp;&nbsp;<input type="submit" value="Apply"/ +></td> </form> </tr> <tr> <form action="/goform/formHandlerConfigureActive" method="get" + onsubmit="return onSubmitActive('Warning: Incorrect parameters may c +ause undesired behavior. To save the new setting, click Ok.')"> <td>&nbsp;</td> <td>Active:</td> <td><select id="oSelectActive" name="active"><option value="0" +>0</option><option value="1">1</option><option value="2">2</option><o +ption value="3">3</option></select>&nbsp;&nbsp;<input type="submit" v +alue="Apply"/></td> </form> </tr> <tr> <form action="/goform/formHandlerConfigure" method="get"> <td>&nbsp;</td> <td>choose Profile to be configured:</td> <td><select id="oSelectChoose" name="Nr"><option value="1">1</ +option><option value="2">2</option><option value="3">3</option></sele +ct>&nbsp;&nbsp;<input type="submit" value="Show"/></td></td> </form> </tr> </table> <fieldset> <legend><span class="styleColorSpan"><b>Profile 1 </b></span>< +/legend> <form action="/goform/formHandlerConfigureSettings" method="post" +onsubmit="return onSubmit(this,'Please complete the red entry.','Warn +ing: Incorrect parameters may cause undesired behavior. To save the n +ew settings, click Ok.','0')"> <table width="100%" align=middle bgColor=#ffffff vAlign=top> <tr width="100%" cellspacing="0" cellpadding="0"> <td width="5%"></td> <td width="45%">Name:</td> <td width="50%"><input type="text" name="Name" /></td> </tr> </table> <fieldset> <legend><span class="styleColorSpan"><b>Settings</b></span></l +egend> <table width="100%" align=middle bgColor=#ffffff vAlign=top> <tr width="100%" cellspacing="0" cellpadding="0"> <td width="5%"><img src="../invis.gif" alt="" width="50" heigh +t="30"/></td> <td width="45%">Service Set:</td> <script type="text/JavaScript"> var myVar_IfWired = "1"; if(myVar_IfWired != 0){ var myCounter = 0; var myCounter1 = "10"; var aspgetstringval = "TEST1zirgtspghwqTEST2zirgtspghwqTES +T3zirgtspghwqTEST4zirgtspghwqTEST5zirgtspghwq"; var mySplitResult = aspgetstringval.split("zirgtspghwq"); document.writeln('<td><select id="oSelectType" nam +e="S" onchange="onSelectType()">'); document.writeln('<option value="NONE">Select Opti +on</option>'); while(myCounter < myCounter1){ var webpagedisplay = mySplitResult[myCounter]; document.writeln('<option value="'+mySplitResult[m +yCounter]+'">'+webpagedisplay+'</option>'); myCounter++; } document.writeln('<option value="MANUAL">CONFIGURE MANUALL +Y</option>'); document.writeln('</select></td>'); } function toggle2(id, link) { var e = document.getElementById(id); if (e.style.display == "") { e.style.display = "none"; link.innerHTML = "<b> [+] ADVANCE SETTING </b>"; } else { e.style.display = ""; link.innerHTML = "<b> [-] ADVANCE SETTING </b>"; } } </script> </tr> <tr> <!--td>&nbsp;</td> <td> Type:</td> <td><select id="oSelectType" name="Sec" onchange="onSelectType +()"><option value="NONE">No <option value="WEP">WEP<option value="WPA +">WPA</select></td--> </tr> </table> <span id="oSpanManualConfig"> <table width="100%" align=middle bgColor=#ffffff vAlign=top> </table> </span> </fieldset> <a href="#" class="styleColorSpan" onclick="toggle2('content', thi +s)"><b>[+] ADVANCE SETTINGS</b></a> <fieldset id="content" style="display:none" > <legend><span class="styleColorSpan"><b> Advance Setting</b></ +span></legend> <fieldset> <legend><span class="styleColorSpan"><b>Pror</b></span></legen +d> <table width="100%" align=middle bgColor=#ffffff vAlign=top> <tr> <td>&nbsp;</td> <td>ver Port:</td> <td><input id="oTextProxy" type="text" name="Prox" value="" ma +xlength="5" onKeyUp="onKeyUpText()"/></td> </tr> </table> </fieldset> <fieldset> <legend><span class="styleColorSpan"><b>ngs</b></span></legend +> <table width="100%" align=middle bgColor=#ffffff vAlign=top> <tr> <td width="45%">Use DHCP:</td> <td width="50%"><input id="oCheckboxDhcp" type="checkbox" name +="DHCPt" value="" onclick="onClickCheckboxDhcp()"> DHCP</input></td> </tr> </table> </fieldset> </fieldset> <table > <tr width="100%" cellspacing="6" cellpadding="6"> <td width="50%"><input type="submit" name=button value="Apply" +/></td> </tr> </table> </form> </fieldset> </td> </tr> </table> </body> </html>

      Hope this gives you much information to work around the problem.

        Ouch!

        Looking at the source code as Firefox renders it, the problem becomes apparent immediately:

        <form onsubmit="return validateDMPNameOnSubmit(this)" method="post" ac +tion="/goform/formHandlerConfigureDMPName"></form> <td width="5%"> <td width="45%">Digital Media Player Name:</td> <td width="50%"> <input type="text" maxlength="129" value="mmmmmmmm" name="DMPName"> <input type="submit" value="Apply"> </td>

        Firefox happily (and likely in accordance with some HTML specification) moves all the input elements outside of the form, as the form nastily spans across <td> elemenst without respecting their allowed nesting.

        As WWW::Mechanize::Firefox relies on Firefox doing the heavy lifting, there is nothing I can do. Maybe you can work around things by specifying explicit XPath expressions to get at the respective DOM elements.

        Your post highlighted another error - currently, ->form_with_fields will not respect <select> or <textarea> elements - this will be changed in the next version.