I am using PyAutoGUI library of Python to automate GUI. The application which I am automating opens a new window after I am done with data entry on my current window. Everything is taken care by python automation (data entry in my current window and the click required to open the window).
When the click is performed in the current window, the new window takes some time to open (which may range from 2 – 5 seconds). So there are two options that I can think of here:
- Sleep using
time.sleep(5)
(Con: 3 seconds might be wasted unnecessarily) - Spin in a tight loop till the window appears on the screen. PyAutoGUI offers a
locateOnScreen
function which could be used to find out if the window has actually appeared on the screen. (However, this is CPU intensive and the function itself is CPU intensive and takes almost 2 seconds to return)
So it looks [1] is a better option to me. Is there some other technique that I may have missed that would be better than either of these two methods? Thanks.
Advertisement
Answer
For Windows only GUI automation pywinauto is a bit more full-featured (and pythonic). It waits some default time implicitly and allows explicit waiting (some CPU-non-intensive loop is inside: kind of 0.1 sec pause and quick check for changes, then waiting again).
PyAutoGUI locateOnScreen
function uses advanced analysis of the screenshot. That is why it’s so CPU intensive (but cross-platform).
pywinauto example:
from pywinauto import Application app = Application(backend="win32").start(u'your_app.exe') app.MainWindow.menu_select(u'File->Open') app.OpenDialog.Edit.set_edit_text(u'some path') app.OpenDialog.Open.click() app.OpenDialog.wait_not('visible', timeout=10) new_main_window = app.window(title_re='^.* - The Software$') new_main_window.wait('ready', timeout=15)
Getting Started Guide is a good starting point for learning pywinauto core concept.