windows 桌面GUI自动化- 13.pywinauto 等待方法wait() 和 wait_not()

前言

pywinauto 提供了2种等待方法

  • wait() 等待窗口达到指定状态
  • wait_not() 等待窗口不处于某种状态

wait() 等待

wait() 相关源码

    def wait(self, wait_for, timeout=None, retry_interval=None):
        """
        Wait for the window to be in a particular state/states.

        :param wait_for: The state to wait for the window to be in. It can
            be any of the following states, also you may combine the states by space key.

             * 'exists' means that the window is a valid handle
             * 'visible' means that the window is not hidden
             * 'enabled' means that the window is not disabled
             * 'ready' means that the window is visible and enabled
             * 'active' means that the window is active

        :param timeout: Raise an :func:`pywinauto.timings.TimeoutError` if the window
            is not in the appropriate state after this number of seconds.
            Default: :py:attr:`pywinauto.timings.Timings.window_find_timeout`.

        :param retry_interval: How long to sleep between each retry.
            Default: :py:attr:`pywinauto.timings.Timings.window_find_retry`.

        An example to wait until the dialog
        exists, is ready, enabled and visible: ::

            self.Dlg.wait("exists enabled visible ready")

        .. seealso::
            :func:`WindowSpecification.wait_not()`

            :func:`pywinauto.timings.TimeoutError`
        """

wait_for 可选参数:

  • 'exists' 表示窗口存在,是一个有效的句柄
  • 'visible' 表示窗口可见
  • 'enabled' 表示窗口未被禁用
  • 'ready' 表示窗口可见且已启用
  • 'active' 表示窗口处于活动状态

timeout:超时时间
retry_interval:表示重试间隔

使用示例

from pywinauto import Application


app = Application('uia').start("notepad.exe")
win = app.window(title_re="无标题 - 记事本")
# 输入内容
win.child_window(title="文本编辑器").set_text("hello world")

# 文件-另存为
win.menu_select('文件(F) -> 另存为(A)...')

# 等待另存为窗口出现
win.child_window(title="另存为", control_type="Window").wait('ready', timeout=5)

wait_not()

与wait() 刚好相反,等待不处于某种状态

posted @ 2023-08-24 15:56  上海-悠悠  阅读(742)  评论(0编辑  收藏  举报