Python机械化 - 选择一个值并提交不起作用

时间:2013-12-03 11:21:59

标签: javascript python mechanize form-submit

HTML代码看起来像这样(它旁边有一个按钮,可以选择一个值):

<input type="text" name="account_name" class="sqsEnabled" tabindex="0" id="account_name" size="" value="" title='' autocomplete="off">
<input type="hidden" name="account_id" id="account_id" value="">

<button type="button" name="btn_account_name" id="btn_account_name" tabindex="0" title="Select Account" class="button firstChild" value="Select Account"
onclick='open_popup("Accounts", 600, 400,"",true, false,{"call_back_function":"set_return","form_name":"EditView","field_to_name_array":"id":"account_id","name":"account_name"}}, 
"single", true);' >
.....

#the submit button
<input title="Save" accesskey="a" class="button primary" onclick="var _form = document.getElementById('EditView'); _form.action.value='Save'; if(check_form('EditView'))SUGAR.ajaxUI.submitForm(_form);return false;" type="submit" name="button" value="Save" id="SAVE_HEADER">

当我选择值时,HTML会修改上一行,如下所示:

<input type="hidden" name="account_id" id="account_id" value="30a4f430-5b15-8d7f-632a-52723fb0921a">

并且在name =“account_name”的输入中有一个字符串值“XXX LTD”

所以我想如果会这样做,我会成功提交表格:

    br.select_form(nr=0)
    br.form.set_all_readonly(False)
    form = br.form
    form['account_name'] = "XXX LTD"
    form['account_id'] = "30a4f430-5b15-8d7f-632a-52723fb0921a"
    #other forms ...
    response = br.submit()

Python没有错误,但表单未提交。有任何想法吗?提前致谢

2 个答案:

答案 0 :(得分:0)

Mechanize不支持JavaScript。为什么你有这个标签?

你没有选择表格。通过以下方式获取您的选择:

br.select_form(nr=0)

答案 1 :(得分:0)

问题是您创建了一个词典form并且没有使用它,请尝试:

form = br.form
form['account_name'] = "XXX LTD"
form['account_id'] = "30a4f430-5b15-8d7f-632a-52723fb0921a"
br.form = form # <--- here
response = br.submit()