Get Mechanize to handle cookies from an arbitrary POST (to log into https://www.t-mobile.com/ progra
- by Horace Loeb
I want to log into https://www.t-mobile.com/ programmatically. My first idea was to use Mechanize to submit the login form:
However, it turns out that this isn't even a real form. Instead, when you click "Log in" some javascript grabs the values of the fields, creates a new form dynamically, and submits it.
"Log in" button HTML:
<button onclick="handleLogin(); return false;" class="btnBlue" id="myTMobile-login"><span>Log in</span></button>
The handleLogin() function:
function handleLogin() {
    if (ValidateMsisdnPassword()) { // client-side form validation logic
        var a = document.createElement("FORM");
        a.name = "form1";
        a.method = "POST";
        a.action = mytmoUrl; // defined elsewhere as https://my.t-mobile.com/Login/LoginController.aspx
        var c = document.createElement("INPUT");
        c.type = "HIDDEN";
        c.value = document.getElementById("myTMobile-phone").value; // the value of the phone number input field
        c.name = "txtMSISDN";
        a.appendChild(c);
        var b = document.createElement("INPUT");
        b.type = "HIDDEN";
        b.value = document.getElementById("myTMobile-password").value; // the value of the password input field
        b.name = "txtPassword";
        a.appendChild(b);
        document.body.appendChild(a);
        a.submit();
        return true
    } else {
        return false
    }
}
I could simulate this form submission by POSTing the form data to https://my.t-mobile.com/Login/LoginController.aspx with Net::HTTP#post_form, but I don't know how to get the resultant cookie into Mechanize so I can continue to scrape the UI available when I'm logged in.
Any ideas?