'Submitting a form using Requests Python

So I want to automate the filling in and submitting of a form and I am using Requests to do that.

From inspecting elements I know the url to submit to and the type of submition (post):

method="post"
action="/sformsubmit"
enctype="multipart/form-data"

My problem is that my request is not going through and being fairly new to this I am not sure why.

On my webpage I have two buttons side by side like so :

   ___________________________          ________________________________
   |    Submit decleration   |          |        Reset Form            |
   ___________________________          ________________________________

And when I inspect elements on that line i get:

<td align="center" colspan="2">
    <input type="hidden" name="inLeader" value>
    <input type="hidden" name="inMember" value>
    <input type="hidden" name="version" value="0">
    <input type="hidden" name="key" value="2013:a:c:3:2s">
    <input type="submit" value="Submit declaration">
    <input type="reset" value="Reset form">
</td>

I am trying the following:

>>> payload = {'inLeader':'', 'inMember':'', 'version':'0', 'key':'2013:a:c:3:2s'}

However it doesnt seem to work and isnt generating any errors.

Any help would be grateful. Thanks in advance



Solution 1:[1]

You can use mechanical soup!

import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.open("https://YOURSITE.example") #<Response [200]>
browser.select_form('form CSS selector') #Right Click on element in inspect and select copy as CSS selector
browser["inLeader"] = ""
browser["inMember"] = ""
#and more!
response = browser.submit_selected()

Learn more : https://mechanicalsoup.readthedocs.io/en/stable/tutorial.html

Good Job!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The Erish