Python mechanize - two buttons of type 'submit'

前端 未结 4 1459
刺人心
刺人心 2021-02-02 09:42

I have a mechanize script written in python that fills out a web form and is supposed to click on the \'create\' button. But there\'s a problem, the form has two buttons. One fo

4条回答
  •  失恋的感觉
    2021-02-02 10:19

    I would suggest you to use Twill which uses mechanize (mostly monkeypatched). So say you have form with some fields and two submit buttons with names "submit_to_preview" and "real_submit". Following code should work.

    BTW remember this is not threadsafe so you might want to use locks in case if you want to use the code in a threaded env.

    import twill.commands
    b = twill.get_browser()
    url = "http://site/myform"
    twill.commands.go(url)
    twill.commands.fv("2", "name", "Me")
    twill.commands.fv("2", "age", "32")
    twill.commands.fv("2", "comment", "useful article")
    twill.commands.browser.submit("real_submit")
    

    Hope that helps. Cheers.

提交回复
热议问题