pyAutoGui无法在Adobe安装弹窗等特定窗口执行点击如何解决
问题描述
编写Python程序实现新电脑的软件自动安装配置,使用pyAutoGui在弹出安装提示时点击Install按钮,但始终无法成功触发点击。
相关现象如下:
- 程序可正常检测到
Install.png图像,Adobe弹出安装提示时会正常退出图像检测循环 - 在画图、Word等普通窗口测试时,将匹配图像替换为对应窗口内的元素,可直观看到点击操作正常生效
- 打印识别到的坐标后,通过DisplayMouseInfo核对确认坐标完全准确,甚至硬编码坐标测试依然无法点击Install按钮
已尝试的无效方案:
- 使用mouse库移动到指定坐标后点击
- 测试单击/双击/三击多种点击模式
- 测试
MouseDown→等待→MouseUp组合、直接MouseDown+MouseUp的点击逻辑 - 通过pyAutoGui发送回车
- 通过shell发送回车
- 调用subprocess执行操作
- 通过win32gui枚举窗口,将标题为
Adobe Acrobat Reader DC (Continuous) - Setup的安装窗口置为前台后再执行点击、发送回车操作,调用窗口置顶接口时出现权限拒绝报错
测试代码
基础版本代码
import os import shutil import subprocess import psutil import pyautogui as pg import time global path path = os.getcwd() def adobe_install(): adobe_setup_path = os.path.join(path, "Adobe.exe") adobe_cmd = str("start " + adobe_setup_path) os.system(adobe_cmd) while pg.locateCenterOnScreen('Install.png', confidence=0.9) == None: wait time.sleep(2) clk= pg.locateCenterOnScreen('Install.png', confidence=0.9) pg.click(clk.x, clk.y)
加入窗口控制逻辑的测试版本
补充说明:
Install.png为Install按钮的原生截图,打印输出的坐标clk.x、clk.y完全准确;代码中用到的点击、左键点击、发送回车操作,在画图、Word等其他窗口中均可正常生效。
import os import shutil import subprocess import pyautogui as pg import time import pathlib import winreg import win32con import win32gui def adobe_install(): adobe_setup_path = os.path.join(path, "Adobe.exe") adobe_cmd = str("start " + adobe_setup_path) os.system(adobe_cmd) while pg.locateCenterOnScreen('Install.png', confidence=0.9) == None: wait results = [] top_windows = [] win32gui.EnumWindows(windowEnumerationHandler, top_windows) for i in top_windows: if "Adobe Acrobat Reader DC (Continuous) - Setup" in i[1]: print(i) win32gui.ShowWindow(i[0],5) win32gui.SetForegroundWindow(i[0]) bring_to_front(i[0]) break time.sleep(2) clk= pg.locateCenterOnScreen('Install.png', confidence=0.9) print(clk.x) print(clk.y) pg.click(clk.x, clk.y) pg.leftClick(x=clk.x, y=clk.y) pg.typewrite(['enter'], interval=1) adobe_install()
报错信息
File "C:\Users\***\documents\ict\software\New_comp.py", line 77, in adobe_install bring_to_front(i[0]) File "C:\Users\***\documents\ict\software\New_comp.py", line 41, in bring_to_front win32gui.SetWindowPos(HWND, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE) pywintypes.error: (5, 'SetWindowPos', 'Access is denied.')
内容的提问来源于stack exchange,提问作者Dylan
相关产品推荐
相关产品推荐

