如何在Mechanize/Python中设置隐藏表单的值?

2024-06-01 11:24:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在抓取一个网站,使用一个隐藏的形式作为一种手段的对策,正是我试图做的。此表单:

<input style="width: 2px; height: 25px" type="hidden" size="1" name="TestJavaScript" /> 

才是罪魁祸首。表单希望此输入的值将由稍后在下面执行的一些JavaScript设置为“OK”:

function doSignOn() {
    window.document.tether.method = "POST";
    window.document.tether.action = "https://missionlink.missionfcu.org/MFCU/login.aspx";
    window.document.tether.TestJavaScript.value = "OK";

    if (window.document.tether.user.value.length < 1) {
        alert("Please enter your Member Number.");
        return;
    }

    if (window.document.tether.PIN.value.length < 1) {
        alert("Please enter your Password.");
        return;
    }

    // If we're in the service interruption or notice window, put up an alert.
    if (now <= interruption_end) {
        if (now >= notice_begin) {
            alert(prewarn_alert+'\n\nThank you.');
        }
    }
    window.document.tether.submit();
}

很聪明。我正在使用mechanize刮页,如何设置此表单项的值?当我用Python打印form对象时,如下所示:

<tether POST https://missionlink.missionfcu.org/MFCU/login.aspx application/x-www-form-urlencoded
  <TextControl(user=)>
  <PasswordControl(PIN=)>
  <HiddenControl(TestJavaScript=) (readonly)>
  <SelectControl(signonDest=[*My Default Destination, Accounts.Activity, Accounts.Summary, Transfers.AddTransfer, SelfService.SelfService])>
>

因为它显示为“只读”,所以我无法修改它,否则它会抛出异常。当然有解决办法,对吧?有什么想法吗?


Tags: httpsorg表单ifvalueokalertwindow
1条回答
网友
1楼 · 发布于 2024-06-01 11:24:24

如所贴elsewhere(即在mechanize库的FAQ页面上):

form.find_control("foo").readonly = False # allow changing .value of control foo 
form.set_all_readonly(False) # allow changing the .value of all controls

相关问题 更多 >