在机械化中提交表格 [英] Submitting a form in mechanize

查看:120
本文介绍了在机械化中提交表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有提交表单提交结果的问题(我可以提交表单,但我无法在第一个表单的页面上提交表单)。



我有:

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

browser.form ['j_username'] ='用户名'
browser.form ['j_password'] ='密码'
req = browser.submit()

这适用于 print req 结果

`

 < body onload =document.forms [0] .submit()> 
< noscript>
< p>
< strong>注意:< / strong>由于您的浏览器不支持JavaScript,
,您必须按一下继续按钮才能继续。
< / p>
< / noscript>

< div>
< input type =hiddenname =RelayStatevalue =cookie:95ca495c/>

< input type =hiddenname =SAMLResponsevalue =真正的长编码值/>
< / div>
< noscript>
< div>
< input type =submitvalue =Continue/>
< / div>
< / noscript>
< / form>

< / body>



`



我尝试使用 req.select_form(nr = 0)



我假设这可能来自沿线如何机械化返回来自 submit()的对象,并且我以错误的方式回答这个问题。



任何输入或指导将不胜感激:)

解决方案

再试一次 browser.select_form(nr = 0)而不是 req.select_form(nr = 0)。 (在提交或点击链接之后,新的回复被认为是一个实际的浏览器页面 - 就像在浏览器中一样:))


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

解决方案

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

这篇关于在机械化中提交表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆