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?
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()
来源:https://stackoverflow.com/questions/26263323/python-mechanize-form-submitting-doesnt-work