2012-12-27 37 views
0

我有一個包含文本字段,下拉菜單和複選框的java腳本的HTML表單。我想用python代碼填充這個html表單。表單有兩個按鈕之一是「應用」,第二個是「提交更改」,首先點擊應用然後提交更改。 HTML代碼是這樣的:複選框和文本區域與機械化

<form id="my_form" action="/temp/var/index" method="POST"> 
<fieldset id="my_fieldset" style="width:1330px"> 

    <h3 style="margin:1px;margin-left:20px;"> MY Form <div class='helps' id='helpform'> </div></h3> 
    <p><label for="help_no" id="label_help" class="help_code" style="width:80px;margin:1px;float:left;text-align:right;">add a Help:</label> 
    <input type="text" name="help_no" id="help_no" class="text ui-corner-all help_code" style="width:100px;margin:1px;float:left;"/> 
    <label for="helptype" id="label_helptype" class="help_code" style="width:60px;margin:1px;float:left;text-align:right;">Help type:</label> 
    <select id='helptype' class=" ui-widget-context help_code" style="width:100px;margin:1px;float:left;"> 
     <option value="PHelp">Help1</option> 
     <option value="THelp">Help2r</option> 
     <option value="IHelp">Help3r</option> 
    </select> 
    <p><input type="checkbox" name="comment" id="comment" class=" ui-corner-all help_code" style="margin:1px;margin-left:20px;float:left;"/> 
    <label for="comment" id="label_commment" class="help_code" style="margin:1px;margin-left:3px;clear:left;" >add comment to Helps</label></p> 
    <p><input type='button' id='btn_code' style="width:120px;float:left;margin:10px;margin-top:2px;" value='Apply' disabled/> 
    <p><label style="float:left;margin:1px;text-align:left;color:red;font-weight:bold;">Note:Select results from table, input help information and click Apply.Finally, click Submit Changes.</label></p> 
    <input type='button' id='btn_submit' style="width:120px;float:right;margin:10px;" value='Submit Changes' disabled /> </p> 
</fieldset> 
</form> 

我有嘗試:

import mechanize 

br = mechanize.Browser() 
br.set_handle_robots(False) 
br.open('http://myform.com/') 
br.select_form(name="my_form") 
br['help_no']='12345' 

res = br.submit() 

,但我不知道如何設置下拉菜單中的價值和複選框。 如果這樣做沒有更好的辦法,請讓我知道...

回答

0

您應該使用選定的form

br.form['help_no'] = '12345' 
br.form['helptype'] = ['THelp'] # a list, so you could have multiple selected 
br.form.find_control('comment').items[0].selected = True