Python automate key press of a QtGUI
<p>I have a python script with the section below:</p>

<pre><code>for index in range(1,10):
   os.system('./test')
   os.system('xdotool key Return') 
</code></pre>

<p>What I want to do is to run the executable ./test, which brings up a QtGUI. In this GUI, a key press prompt buton comes up. I want to automate this key press of the GUI so that the executable continues. </p>

<p>My python script though runs the executable, the GUI prompt comes up and a key press is not entered until after the executable. Is there any way to fix this?</p>

<p><code>os.system</code> doesn't return until the child process exits.
You need <code>subprocess.Popen</code>.
It's also a good idea to <code>sleep</code> a while before sending keystrokes
(it may take some time for the child process to get ready to accept user input):</p>

<pre><code>from subprocess import Popen
from time import sleep

for index in range(1,10):
   # start the child
   process = Popen('./test')
   # sleep to allow the child to draw its buttons
   sleep(.333)
   # send the keystroke
   os.system('xdotool key Return')
   # wait till the child exits
   process.wait()
</code></pre>

<p>I'm not shure that you need the last line. If all <strong>9</strong> child processes are supposed to stay alive -- remove it.</p>

python automation qtgui xdotool