I have a python script that sends data to a django app using the requests library. Then the users switch to the web page and click a button which fetches an edit form to add some additional info
I want that immediately after requests recieves a status code 200 it will switch to the web page and click the button automatically, instead of the users doing it manually each time.
I looked into using Selenium but it seems like an overkill. Any thoughts how I can do this?
edit
The current process looks a little like this:
- user runs script on client side
- script runs and collects static data
- script sends data as post using requests
- the data is saved to a model called "Report", with a boolean field called "public" marked as False
- The user switches to the web app
- the user clicks a button that does an ajax call and fetches an edit form for that report (it knows you're the one who sent it by comparing the username of the logged in user)
- the user adds additional data and saves
- the field "public" changes to True and everyone can see the report.
Most of this is working, I just want the script to automatically switch to the web page and click the button instead of it being done manually. I know this is a little convoluted but I hope it explains things better
Also I'm using Windows and Chrome as my web browser
Second Edit
So I built a little demo to play around with. I created a file named 'test.html' which looks like this:
<html>
<head>
<title>Test</title>
<script type='javascript/text' src='jquery.js' ></script>
</head>
<body>
<div class="container">
<button id="button1"> Fake </button>
<button id="button2"> Fake </button>
<button id="button3"> Real </button>
</div>
<script>
$(function() {
$('#button3').on('click', function() {
alert('you found it!');
});
});
</script>
</body>
</html>
As you can see, the only way to run the script is to click on the "real" button. Now I have written a python script which brings it up into the screen:
import win32gui
class Window(object):
def __init__(self, handle):
assert handle != 0
self.handle = handle
@classmethod
def from_title(cls, title):
handle = win32gui.FindWindow(title, None) or win32gui.FindWindow(None, title)
return cls(handle)
chrome = Window.from_title('Test - Google Chrome')
win32gui.SetForegroundWindow(chrome.handle)
win32gui.SetFocus(chrome.handle)
Now how do I get it to replicate a button click done by the user? There probably is a way to do it graphically using coordinates but is that the only way? And how do you assure the button will always be at the same spot? Is there a better way than to use a button?