Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting a form in mechanize

I'm having issues submitting the result of a form submission (I can submit a form, but I can't submit the form on the page that follows the first).

I have:

browser = mechanize.Browser()
browser.set_handle_robots(False)
browser.open('https://www.example.com/login')
browser.select_form(nr=0)

browser.form['j_username'] = 'username'
browser.form['j_password'] = 'password'
req = browser.submit()

This works, as print req results in

`

<body onload="document.forms[0].submit()">
    <noscript>
        <p>
            <strong>Note:</strong> Since your browser does not support JavaScript,
            you must press the Continue button once to proceed.
        </p>
    </noscript>

    <form action="https://www.example.com/Shibboleth.sso/SAML2/POST" method="post">
        <div>
            <input type="hidden" name="RelayState" value="cookie:95ca495c"/>                

            <input type="hidden" name="SAMLResponse" value="really long encoded value"/>                
        </div>
        <noscript>
            <div>
                <input type="submit" value="Continue"/>
            </div>
        </noscript>
    </form>

</body>

`

But I get errors when I try to use req.select_form(nr=0)

I assume this is probably from something along the lines of how mechanize returns objects from submit() and that I'm going about this the wrong way.

Any input or guidance would be appreciated :)

like image 655
Parker Avatar asked Oct 18 '10 02:10

Parker


1 Answers

try again browser.select_form(nr=0) instead of req.select_form(nr=0). (after submitting or clicking a link or so, the new response is considered as an actual browser page - like in a browser :) )

like image 84
mykhal Avatar answered Oct 15 '22 03:10

mykhal