Windows Desktop GUI Automation using Python - Sleep vs tight loop

ぃ、小莉子 提交于 2019-12-05 02:15:30

问题


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:

  1. Sleep using time.sleep(5) (Con: 3 seconds might be wasted unnecessarily)
  2. 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.


回答1:


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.




回答2:


I would go with option one but I would sleep for 2 seconds if that is the minimum average time required the open a window. After 2 seconds, I would check if the window has appeared and if not, then I would sleep again for 2 seconds. That would possibly save more time than sleeping for 5 seconds.

But since trying to check for the window is CPU intensive and time consuming, I think waiting for 5 seconds would be better over all.



来源:https://stackoverflow.com/questions/34361728/windows-desktop-gui-automation-using-python-sleep-vs-tight-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!