Python mechanize form submitting doesn't work

我只是一个虾纸丫 提交于 2019-12-10 10:29:34

问题


I am trying to write a simple bot that would login to my account on a page and then comment other users' images. However I am not able to get the comment form submitting work correctly. The comment form looks like this:

<form id="comment-form" action="#" onsubmit="postComment($(this).serialize(),'image',117885,229227); return false;">
    <input class="comment" type="text" size="40" name="comment" id="comment" />
    <input type="hidden" name="commentObj" value="9234785" />
    <input type="hidden" name="commentMode" value="image" />
    <input type="hidden" name="userid" value="12427" />
    <input class="submit" type="submit" value="Comment" />
</form>

My code is as follows

br.select_form(nr = 1)
br.form['comment'] = 'hello'
br.submit()

The page has two forms and the comment form is the second one. So I am sure I have selected the correct form. Can anyone explain why this doesn't work?


回答1:


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.




回答2:


If I understood well you must try with this changed code

br.select_form(nr = 1)
br['comment'] = 'hello'
br.submit()


来源:https://stackoverflow.com/questions/26263323/python-mechanize-form-submitting-doesnt-work

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