Thursday, 8 August 2013

Python adding a new control gives error

Python adding a new control gives error

I am using mechanize in Python . I am unable to set the value of a
SelectControl element.
The webpage I am trying to crawl is
http://www.mcxindia.com/SitePages/indexhistory.aspx the part source code
which I am concerned about is :
<tr>
<td bgcolor="#f5f4f5" class="txtblue"><table
border="0" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td><table id="mRbtLstSpotFut"
border="0"
style="font-weight:normal;height:85px;width:208px;">
<tr>
<td><input id="mRbtLstSpotFut_0" type="radio"
name="mRbtLstSpotFut" value="1" checked="checked"
/><label for="mRbtLstSpotFut_0">Commodity Future
Indexes</label></td>
</tr><tr>
<td><input id="mRbtLstSpotFut_1" type="radio"
name="mRbtLstSpotFut" value="0"
onclick="javascript:setTimeout('__doPostBack(\'mRbtLstSpotFut$1\',\'\')',
0)" /><label for="mRbtLstSpotFut_1">Commodity Spot
Indexes</label></td>
</tr><tr>
<td><input id="mRbtLstSpotFut_2" type="radio"
name="mRbtLstSpotFut" value="2"
onclick="javascript:setTimeout('__doPostBack(\'mRbtLstSpotFut$2\',\'\')',
0)" /><label for="mRbtLstSpotFut_2">Rainfall
Indexes</label></td>
</tr>
</table></td>
<td>&nbsp;</td>
<td rowspan="3" valign="top"><p
style="margin-top: 3px; margin-bottom:
0; margin-left: 0" align="justify">
<table width="100%" border="0"
cellspacing="0" cellpadding="3">
<tr>
<td class="tablerowtxt1"> Selected
Index</td>
<td>&nbsp;</td>
<td colspan="2"><select
name="mDdlOtherIndex"
id="mDdlOtherIndex" class="dd"
style="width:170px;">
<option selected="selected" value="323">MCXCOMDEX</option>
<option value="324">MCXMETAL</option>
<option value="325">MCXENERGY</option>
<option value="326">MCXAGRI</option>
</select></td>
Now I can change the value of the 2nd radio button which I am doing by
br.select_form(nr=0)
br.set_all_readonly(False)
br.form['mTbFromDate']='08/01/2013'
br.form['mTbToDate']='08/08/2013'
br.form.set_value(['0'],name='mRbtLstSpotFut')
But I also want to select the option value '327' which is not
present(since it comes up by JS when the radio button is clicked, which
mechanize cannot handle)
So I am trying to add a new control to the form like
<option value="327">MCXENERGY</option>
I am trying this, but this does not work.
br.form.new_control('text','option',{'value':'327'})
br.form.fixup()
br.form['mDdlOtherIndex']= ['327']
But this gives me the following error:
File "build\bdist.win-amd64\egg\mechanize\_form.py", line 2006
mechanize._form.ItemNotFoundError: insufficient items with name '327'
Where am I going wrong?

No comments:

Post a Comment