开发者

How to set value of hidden form in Mechanize/Python?

I'm scraping a site that uses a hidden form as a means of a countermeasure against exactly what I'm trying to do. This form:

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

is the culprit. The form expects that this input's value will be set to "OK" by some JavaScript that executes later on down the line:

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 se开发者_Python百科rvice 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();
}

Clever. I'm using mechanize to scrape the page, how can I set the value of this form item? When I print the form object in Python, here's what it looks like:

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

As it shows up as "read only", I can't modify it, else it throws an exception. Surely there's a workaround, right? Any ideas?


As posted elsewhere (namely on the mechanize library's FAQ page):

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜