click submit button "OK" on a webpage using python requests module

user3515946 picture user3515946 · Oct 15, 2014 · Viewed 8.8k times · Source

i have a device that can be managed using webpage interface.

the device address can be :

http://10.18.25.25/restart

with a submit button in the webpage:

<form action="/restart/restart" method="POST" name="restartForm">
                            <input type="submit" value="OK">
                        </form>

i am trying to use python module requests to automate clicking on the button on that webpage.

from urllib3 import requests
r = requests.get('http://10.18.25.25/restart', auth=('username', 'password'))

any ideas??

Answer

Darren Ringer picture Darren Ringer · Oct 15, 2014

Depending on the site you may or may not be able to emulate the form's action with an appropriate HTTP POST request to /request/request, but in the majority of real-world examples I have seen there are additional cookies and javascript that runs alongside the form making this approach extremely difficult if not impossible.

However, check out the Selenium library, which provides commands to operate a browser programatically via Python. I know your specific question is for the requests module, and in your case that may suffice perfectly, but in the general case Selenium might be worth checking out.