2015-06-26 105 views
1

我的表單HTML源代碼如下,我試圖「檢查其中一個複選框」並點擊「更新」或使用機械化提交按鈕。我將如何去做這件事? linear_entitlements的變量是否無法實現?Python機械化提交表單數據

<form accept-charset="UTF-8" action="/admin/users/3548003/user_linear_entitlements" class="form with-border" id="edit_user_3548003" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden" value="samplevalue"></div> 
 
    <table class="table table-striped"> 
 
     <thead> 
 
      <tr> 
 
       <th>Name</th> 
 
       <th>GUID</th> 
 
       <th>CMS Subpack ID</th> 
 
       <th>Last Updated</th> 
 
      </tr> 
 
     </thead> 
 
     <tbody> 
 
      <tr id="linear_entitlement_1"> 
 
       <td> 
 
        <div class="control-group"> 
 
         <label class="checkbox"> 
 
          <span>SUN Pack</span> 
 
          <input class="checkbox" id="user_linear_entitlement_ids_" name="user[linear_entitlement_ids][]" type="checkbox" value="1"> 
 
           </label> 
 
        </div> 
 
       </td> 
 
       <td> 2a59739c-13ed-11e2-a36b-12313d298802 </td> 
 
       <td> 1 </td> 
 
       <td> 2014-02-12 21:32:56 UTC <div style="float:right"><a href="/admin/linear_entitlements/1">→</a></div></td> 
 
      </tr> 
 
      
 
      <tr id="linear_entitlement_7"> 
 
       <td> 
 
        <div class="control-group"> 
 
         <label class="checkbox"> 
 
          <span>Tamil Pack - Legacy</span> 
 
          <input class="checkbox" id="user_linear_entitlement_ids_" name="user[linear_entitlement_ids][]" type="checkbox" value="7"> 
 
           </label> 
 
        </div> 
 
       </td> 
 
       <td> 2ab298dc-13ed-11e2-a36b-12313d298802 </td> 
 
       <td> 3 </td> 
 
       <td> 2015-04-01 23:11:33 UTC <div style="float:right"><a href="/admin/linear_entitlements/7">→</a></div></td> 
 
      </tr> 
 
     </tbody> 
 
    </table> 
 
    <div class="form-actions"> 
 
     <input class="btn primary input_submit" name="commit" type="submit" value="Update">&nbsp;<button type="reset" class="btn">Cancel</button> 
 
    </div> 
 
    
 
</form>

到目前爲止,我有這個,它選擇的形式:

htmlResponse2 = browser.open(URL + 'admin/users/' + old_user_url + '/edit') 
browser.select_form(nr=0) 

回答

2

,如果你需要選擇的形式,我不知道,但如果你這樣做了,以下應該只是伎倆:

br.find_control(type="checkbox").items[0].selected=True 

如果你想選擇所有車ckboxes:

for i in range(0, len(br.find_control(type="checkbox").items)): 
br.find_control(type="checkbox").items[i].selected =True 

然後提交

br.submit() 
+0

它說,它無法找到複選框,也許我選擇的形式不正確。 –

+0

哦這裏... type =「checkbox」 打印表格然後打印find_control進行調試以幫助您。 –