title: Python script to click a web page button
body: <p>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</p>
  
  <p>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.</p>
  
  <p>I looked into using Selenium but it seems like an overkill. Any thoughts how I can do this?</p>
  
  <p><strong>edit</strong></p>
  
  <p>The current process looks a little like this:</p>
  
  <ul>
  <li>user runs script on client side</li>
  <li>script runs and collects static data</li>
  <li>script sends data as post using requests</li>
  <li>the data is saved to a model called "Report", with a boolean field called "public" marked as False</li>
  <li>The user switches to the web app</li>
  <li>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)</li>
  <li>the user adds additional data and saves</li>
  <li>the field "public" changes to True and everyone can see the report.</li>
  </ul>
  
  <p>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</p>
  
  <p>Also I'm using Windows and Chrome as my web browser</p>
  
  <p><strong>Second Edit</strong></p>
  
  <p>So I built a little demo to play around with. I created a file named 'test.html' which looks like this:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Test&lt;/title&gt;
          &lt;script type='javascript/text' src='jquery.js' &gt;&lt;/script&gt;
      &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="container"&gt;
          &lt;button id="button1"&gt; Fake &lt;/button&gt;
          &lt;button id="button2"&gt; Fake &lt;/button&gt;
          &lt;button id="button3"&gt; Real &lt;/button&gt;
      &lt;/div&gt;
  
  
  &lt;script&gt;
  $(function() {
      $('#button3').on('click', function() {
            alert('you found it!');
      });
  });
  
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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 <em>only way</em>? And how do you assure the button will always be at the same spot? Is there a better way than to use a button?</p>