【问题标题】:How to wait for the specific image to appear on screen using pyautogui?如何使用 pyautogui 等待特定图像出现在屏幕上?
【发布时间】:2018-06-01 12:55:49
【问题描述】:

我有一个应用程序可以使用 pyautogui 模块实现自动化。我将参考图像保存在脚本目录中,经过一段时间的睡眠,我能够完成它。在某些版本中,特定图像需要一段时间才能出现。之后我的脚本抛出attribute not found error

我需要知道如何控制 pyautogui 等到图像出现在屏幕上,然后继续执行下一次执行。

我搜索了许多堆栈溢出问题,在脚本等待特定窗口的 pywinauto 模块中找到了 wait_until()。同理,pyautogui 有什么东西可以等待特定图像出现在屏幕上吗?

使用的 Python 版本:3.6。

【问题讨论】:

    标签: python python-3.x pywinauto pyautogui


    【解决方案1】:

    这将等到找到图像:

    icon_to_click = "Recycle Bin"
    
    r = None
    while r is None:
        r = pyautogui.locateOnScreen('rb.png', grayscale = True)
    print icon_to_click + ' now loaded'
    

    【讨论】:

      【解决方案2】:

      在较新版本的 Python (3.6) 中,如果找不到图像,则 locateOnScreen() 不再返回 None。相反,它会引发异常。挣扎了 10 分钟后,我发现下面的解决方案有效:

      r = None
      while r is None:
          try:
              print(pyautogui.locateOnScreen("rb.png"))
              break
          except Exception as e:
              r = None
      

      【讨论】:

        猜你喜欢
        • 2022-01-17
        • 2022-11-04
        • 2016-10-03
        • 2021-03-17
        • 2020-12-11
        • 1970-01-01
        • 1970-01-01
        • 2023-01-14
        • 2018-12-13
        相关资源
        最近更新 更多