Python mechanize form submitting doesn't work

删除回忆录丶 提交于 2019-12-06 11:59:12

There is a javascript code being executed while the form submit is happening:

onsubmit="postComment($(this).serialize(),'image',117885,229227); return false;"

mechanize simply cannot handle it since it is not a browser and it doesn't have a javascript engine inside.

Possible solutions:

  • a high-level approach - use a real browser through selenium webdriver and automate use actions - send keys to an input, clicking submit button etc. Example code:

    from selenium import webdriver
    
    driver = webdriver.Firefox()
    dirver.get('my_url_here')
    
    comment = driver.find_element_by_id('comment')
    comment.send_keys('hello')
    comment.submit()  # this would find an enclosing form and submit it
    
  • research what request(s) is (are) being sent to the server while the form submit event is being fired. Then, automate the request(s) using, for example, requests.

Hope that helps.

If I understood well you must try with this changed code

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