Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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();
}

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?

like image 322
Naftuli Kay Avatar asked Apr 08 '11 07:04

Naftuli Kay


1 Answers

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
like image 161
Naftuli Kay Avatar answered Oct 27 '22 07:10

Naftuli Kay