【问题标题】:PyAutoGui Fails When I Open Putty with a Script当我使用脚本打开 Putty 时 PyAutoGui 失败
【发布时间】:2021-09-24 14:08:36
【问题描述】:

问题

我正在使用 Python 自动执行 putty 会话。我正在使用 PyAutoGui 加载我保存的会话。当我手动打开腻子,然后运行脚本时,脚本运行良好。但是,当我将 Popen(putty.exe) 添加到我的代码中时,我收到以下错误。我不确定为什么当我手动打开腻子而不是在脚本中打开它时,locateOnScreen 会起作用。我尝试使用打印屏幕和pyautoguis截图功能代替sn-p工具,但没有奏效。

错误

Traceback (most recent call last):
  File "C:\filepath\main.py", line 14, in <module>
    session_center = pyautogui.center(saved_session)
  File "C:\filepath\site-packages\pyscreeze\__init__.py", line 582, in center
    return Point(coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2))
TypeError: 'NoneType' object is not subscriptable

代码

import pyautogui
import os
from subprocess import Popen


home = os.path.expanduser('~')
corrected_path = home.replace(os.sep, '\\')
log_path = corrected_path + '\\PyAutoGuiPNGs\\'

Popen('putty.exe')

saved_session = pyautogui.locateOnScreen('saved_session.png', grayscale=True, confidence=.5)
session_center = pyautogui.center(saved_session)
pyautogui.moveTo(session_center)
pyautogui.PAUSE = 1
pyautogui.click()

pyautogui.write('router')
load = pyautogui.locateOnScreen('load.png', grayscale=True, confidence=.5)
load_center = pyautogui.center(load)
pyautogui.moveTo(load_center)
pyautogui.PAUSE = 1
pyautogui.click()

open_button = pyautogui.locateOnScreen('open.png', grayscale=True, confidence=.5)
open_center = pyautogui.center(open_button)
pyautogui.moveTo(open_center)
pyautogui.PAUSE = 1
pyautogui.click()

【问题讨论】:

    标签: python putty pyautogui


    【解决方案1】:

    Putty 不会使 PyAutoGUI 崩溃。 PyAutoGUI 未能找到您提供的参考图像,返回 None。然后你要求 PyAutoGUI 计算None 的中心。给定 None 不是一对坐标,并且 center 期望 4 个整数的元组 (left, top, width, height),它无法计算,导致崩溃。

    您可以通过确定您未能找到 saved_session.png 的原因并修改您的目标图像或添加额外的逻辑以允许您找到它来解决此问题。

    【讨论】:

      【解决方案2】:

      好的,我发现了问题。 PyAutoGui 试图在 Putty 窗口打开之前找到它。在 Popen('putty.exe') 之后添加 sleep(0.1) 解决了我的问题

      【讨论】:

        猜你喜欢
        • 2016-08-26
        • 1970-01-01
        • 1970-01-01
        • 2019-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多