Reputation: 18457
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:
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?
Upvotes: 3
Views: 31508
Reputation: 48730
So all you want to do is automatically click a button when the page opens? I'm assuming you only want to auto-click in certain circumstances, so hide the javascript/jquery behind a conditional.
def your_view(request):
# your standard processing
auto_click = request.GET.get('auto_click', False)
if auto_click is not False:
auto_click = True
render('yourtemplate.html', {'auto_click': auto_click })
Then, in your template:
{% if auto_click %}
<script>
$(document).ready(function() {
$('yourbutton').click();
});
</script>
{% endif %}
When the user opens up the webpage, just append "?auto_click=1" to the URL, and you'll get the correct behaviour.
Upvotes: 1