使用Mechanize提交表单进行网页自动化 - 返回错误

0 投票
1 回答
3342 浏览
提问于 2025-04-16 02:54
for control in form.controls:

        if control.type == 'text':
            if 'user' in control.name:
                control.value = 'blah'
            if 'mail' in control.name:
                control.value = 'blah'

        if control.type == 'password':
            if 'pass' in control.name:
                control.value = 'blah'

        if control.type == 'checkbox':
            if 'agree' in control.name:
                control.selected = True

        if control.type == 'submit':
            if 'Submit' in control.name:
                control.readonly = False

我这样填写表单。然后我去选中“同意”这个复选框,完成后我尝试用 br.submit() 来提交表单并发送数据。结果我遇到的错误是:

AttributeError: SubmitControl 实例没有 'click' 这个属性

这是提交和同意控件的 HTML 源代码:

<input type="submit" name="regSubmit" value="Register" />
<label for="regagree"><input type="checkbox" name="regagree" onclick="checkAgree();" id="regagree" class="check" /> <b>I Agree</b></label>

这个特定网站的 HTML 源代码中有这个 JavaScript:

function verifyAgree()
{
    if (document.forms.creator.passwrd1.value != document.forms.creator.passwrd2.value)
    {
        alert("The two passwords you entered are not the same!");
        return false;
    }

    if (!document.forms.creator.regagree.checked)
    {
        alert("Please read and accept the agreement before registering.");
        return false;
    }

    return true;
}
function checkAgree()
{
    document.forms.creator.regSubmit.disabled = isEmptyText(document.forms.creator.user) || isEmptyText(document.forms.creator.email) || isEmptyText(document.forms.creator.passwrd1) || !document.forms.creator.regagree.checked;
    setTimeout("checkAgree();", 1000);
}
setTimeout("checkAgree();", 1000);

当我在 IDLE 中输入 print forms 时,表单显示为已填写,并且所有正确的控件都被选中了。我实在搞不懂为什么这不管用。我已经研究了两天了。

非常感谢任何帮助。

1 个回答

0

为什么不直接像文档示例中那样提交表单呢:

br.select_form(name="whatever")
response = br.submit()

点击提交按钮是一种提交表单的方法;使用JavaScript也是一种方法。所以你不需要费劲去找提交控件。

撰写回答