Submitting a form in mechanize

╄→гoц情女王★ 提交于 2019-12-18 16:10:44

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/3956280/submitting-a-form-in-mechanize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!