Jonathan Allen
Jonathan Allen

Reputation: 151

Python mechanize to submit form data

My form HTML source is below, I am trying to "check one of the checkboxes" and hit the "update" or submit button using mechanize. How would I go about this? Does the variable for linear_entitlements make it not possible?

<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>

So far I have this, which selects the form:

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

Upvotes: 1

Views: 878

Answers (1)

Onur Gokkocabas
Onur Gokkocabas

Reputation: 304

I don't know if you need to select the form, but if you did so, following should just do the trick:

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

if you want to select all checkboxes:

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

then submit

br.submit()

Upvotes: 2

Related Questions