OSError: [WinError -2147221008] 尚未调用 CoInitialize
Can not load UIAutomationCore.dll.
You may need to install Windows Update KB971513.
https://github.com/yinkaisheng/WindowsUpdateKB971513ForIUIAutomation
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "E:/PythonTest/WoniuCBT_FrameWorkV1/mokeytesting/smart_monkey.py", line 94, in start_test
self.random_press()
File "E:/PythonTest/WoniuCBT_FrameWorkV1/mokeytesting/smart_monkey.py", line 69, in random_press
x, y= self.random_pos()
File "E:/PythonTest/WoniuCBT_FrameWorkV1/mokeytesting/smart_monkey.py", line 25, in random_pos
pygame.Rect = window.BoundingRectangle #生成上下左右边界位置 生成Rect对象
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\uiautomation\uiautomation.py", line 5324, in BoundingRectangle
rect = self.Element.CurrentBoundingRectangle
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\uiautomation\uiautomation.py", line 5635, in Element
self.Refind(maxSearchSeconds=TIME_OUT_SECOND, searchIntervalSeconds=self.searchWaitTime)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\uiautomation\uiautomation.py", line 5855, in Refind
if not self.Exists(maxSearchSeconds, searchIntervalSeconds, False if raiseException else DEBUG_EXIST_DISAPPEAR):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\uiautomation\uiautomation.py", line 5805, in Exists
control = FindControl(self.searchFromControl, self._CompareFunction, self.searchDepth, False, self.foundIndex)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\uiautomation\uiautomation.py", line 7614, in FindControl
control = GetRootControl()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\uiautomation\uiautomation.py", line 7397, in GetRootControl
return Control.CreateControlFromElement(_AutomationClient.instance().IUIAutomation.GetRootElement())
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\uiautomation\uiautomation.py", line 50, in instance
cls._instance = cls()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\uiautomation\uiautomation.py", line 61, in __init__
raise ex
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\uiautomation\uiautomation.py", line 56, in __init__
self.IUIAutomation = comtypes.client.CreateObject("{ff48dba4-60ef-4201-aa87-54103eef594e}", interface=self.UIAutomationCore.IUIAutomation)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\comtypes\client\__init__.py", line 238, in CreateObject
obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\comtypes\__init__.py", line 1225, in CoCreateInstance
_ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
File "_ctypes/callproc.c", line 918, in GetResult
OSError: [WinError -2147221008] 尚未调用 CoInitialize。
不知道什么原因,请各位大虾帮忙看看?
# -*- coding: utf-8 -*-
from comtypes import CoInitialize, CoUninitialize
from pymouse import PyMouse
from pykeyboard import PyKeyboard
import uiautomation,random,time,os
from PIL import ImageGrab
import os,time,subprocess
import threading,win32gui
import pygame
#创建一只聪明的猴子
class MokeyTestSmart:
def __init__(self):
self.mouse = PyMouse()
self.keyboard = PyKeyboard()
self.sleep_time = 0.5
#生成随机坐标
def random_pos(self):
#利用UIAutomation获取窗口位置,然后依据这个值生成随机坐标,确保我们的monkey所有操作都是在应用窗口内完成
window = uiautomation.WindowControl(searchDepth=1, ClassName='Notepad')
pygame.Rect = window.BoundingRectangle #生成上下左右边界位置 生成Rect对象
#生成当前窗口内的坐标
x = random.randint(pygame.Rect.left, pygame.Rect.right) #对应left right top bottom
y = random.randint(pygame.Rect.top, pygame.Rect.bottom)
self.mouse.move(x, y)
return x, y
#模拟鼠标单击操作
def random_click(self):
x, y = self.random_pos()
self.mouse.click(x,y)
print("在位置[%d:%d]进行单击操作."%(x,y))
time.sleep(self.sleep_time)
#模拟鼠标双击操作
def random_double_click(self):
x, y = self.random_pos()
self.mouse.click(x=y,y=y,button=1,n=2)
print("在位置[%d:%d]进行双击操作."%(x,y))
time.sleep(self.sleep_time)
#模拟鼠标右击操作
def random_right_click(self):
x, y = self.random_pos()
self.mouse.click(x,y,2)
print("在位置[%d:%d]进行右键操作."%(x,y))
time.sleep(self.sleep_time)
#模拟随机输入
def random_input(self):
x, y=self.random_pos()
content = ['Woniuxy','abcedef','Good Afternoon','Automation','123456','123.45','2019-01-13']
random_content = self.get_random_list(content)
self.mouse.click(x,y)
self.keyboard.type_string(random_content) #输入文字信息
print("在位置[%d:%d]处输入内容:%s"%(x,y,random_content))
time.sleep(self.sleep_time)
#模拟随机按键操作
def random_press(self):
x, y= self.random_pos()
key_list = [self.keyboard.end_key,self.keyboard.control_key,self.keyboard.alt_key,self.keyboard.backspace_key]
random_key = self.get_random_list(key_list)
self.keyboard.press_key(random_key)
self.keyboard.release_key(random_key)
print("在位置[%d:%d]处进行按键操作."%(x,y))
time.sleep(self.sleep_time)
#启动一个应用程序,并完成Monkey测试
def start_test(self,execute,count):
# 执行命令时,使用"start /b"表示在后台运行,不阻塞运行线程
os.system("start /b "+ execute)
time.sleep(5)
for i in range(count):
# 生成一个随机数,用于决定运行哪个操作
seed = random.randint(0,count)
if seed <= count * 0.2:
self.random_click()
elif seed <= count * 0.4:
self.random_double_click()
elif seed <= count * 0.6:
self.random_input()
elif seed <= count * 0.8:
self.random_right_click()
else:
self.random_press()
#停止进程
os.system("taskkill /f /im note.exe")
#公共调用函数 针对list随机取值
def get_random_list(self,args):
random_index = random.randint(0,10000)%len(args)
return args[random_index]
#间隔一定时间来截图,检查进程,并且窗口置顶
def check_runtime(self,interval):
time.sleep(10)
while True:
tasklist = str(subprocess.check_output("tasklist"))
if "notepad.exe" in tasklist:
print("检查进程:程序还在运行中。")
try:
# win32gui.FindWindow(window)
window = uiautomation.WindowControl(searchDepth=1,ClassName="Notepad")
uiautomation.SetForegroundWindow(win32gui.FindWindow("Notepad",None))
except:
print('检查进程:程序已停止运行.')
break
#截图
now = time.strftime("%Y%m%d_%H%M%S",time.localtime())
screen_path = os.path.join(os.getcwd(),'screenshot\\%s_monkey.png'%now)
ImageGrab.grab().save(screen_path)
time.sleep(3)
else:
print("检查进程:程序已停止运行.")
break
if __name__ == '__main__':
monkey = MokeyTestSmart()
time.sleep(2)
# monkey.start_test('notepad.exe',1)
threads = []
threads.append(threading.Thread(target=monkey.start_test,args=('notepad.exe',2)))
threads.append(threading.Thread(target=monkey.check_runtime,args=(2,)))
CoInitialize()
for t in threads:
t.setDaemon(True) #设置子线程为守护线程
CoInitialize()
t.start()
for t in threads:
t.join() #阻塞主线程,让主线程阻塞总时长=其中运行最长时间的子线程的时长
print("主线程执行结束。")
相关推荐
SolidWorks自然语言建模:Python+AI实现CAD语音级自动化
自然语言控制CAD是工程智能化的关键入口,其本质是将模糊的工程语义精准翻译为结构化API调用指令。该技术依赖Python作为胶水语言,深度集成SolidWorks COM接口与大模型推理能力,核心价值在于降低机械设计自动化门槛,让具备基础建模能力的工程师无需掌握VBA或C#即可实现‘说话即建模’。典型应用场景包括标准件快速生成、专利文档驱动三维建模、企业PDM系统自动入库等。本文聚焦SolidWorks Python自动化实践,详解Claude Code轻量验证路径与DeepSeek Harness本地化A
PyAEDT安装失败的5种根因与工程级解决方案
PyAEDT是Ansys Electronics Desktop(AEDT)官方提供的Python API,本质是一个连接用户Python环境与AEDT仿真内核的双向桥接器。其安装失败并非单纯Python包管理问题,而是涉及Windows COM/DCOM通信、ANSYS许可证服务、Intel MKL数学库ABI兼容性及环境变量链路等多层技术耦合。核心原理在于:PyAEDT需同时满足‘被Python识别’和‘能反向控制AEDT进程’两大条件。技术价值体现在自动化电磁仿真流程、CI/CD集成与参数化设计闭环。
OpenClaw数字员工Windows本地部署全指南
数字员工是面向办公自动化的可编程智能体,其核心原理在于将重复性桌面操作(如Excel处理、邮件解析、PDF识别)抽象为可编排的技能单元(Skill),依托本地运行时环境实现低延迟、高安全的端侧执行。技术价值体现在摆脱云端依赖、保护数据隐私、兼容Windows原生接口(如COM、UI Automation),并支持自然语言+YAML+Python多范式定义任务。典型应用场景包括财务报销自动化、律所案卷归档、高校教务辅助等中文办公高频需求。本文聚焦OpenClaw框架在Windows平台的生产级落地,涵盖Pyt
高校技术转移办公室人员如何利用平台提升技术成果的市场转化率?.docx
科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。
人工智能基于Spring AI的流程编排技术:Java开发者实现RAG、工具调用与多Agent协作的系统化方法论
「AI大模型」Spring-AI-流程编排主流做法-保姆级教程 内容概要:本文是一份关于 Spring AI 1.0 GA 版本中流程编排的系统性教程,重点讲解如何利用 Spring AI 的核心机制实现 AI 应用中的标准化、可复用的流程控制。文章从环境搭建入手,深入介绍 ChatClient 作为编排入口的使用方式,并详细阐述 Advisors 拦截链在实现安全过滤、记忆管理、检索增强生成(RAG)、工具调用等功能中的核心作用。进一步覆盖结构化输出、跨进程通信协议 MCP 及基于状态图的复杂工作流引擎 Spring AI Alibaba Graph 等高级编排能力,最后通过一个集成 RAG、工具调用与对话记忆的生产级 Agent 实战案例,展示了全流程自动化的能力。整套体系借鉴了 Servlet Filter 和 LangGraph 思想,强调“约定优于配置”的工程化设计。; 适合人群:具备 Spring Boot 基础,希望系统掌握 AI 应用流程编排的 Java 开发者,尤其是从事企业级 AI 功能开发、Agent 构建及相关平台建设的技术人员。; 使用场景及目标:① 构建具备记忆、RAG、工具调用等复合能力的智能助手;② 实现可插拔、高复用、易测试的 AI 流程组件;③ 设计支持循环、分支和并行的复杂 Agent 工作流;④ 推动跨服务、跨语言的 AI 工具标准化集成(通过 MCP)。; 阅读建议:此资源以 Spring 官方及 Alibaba 生态权威资料为基础,内容紧跟 1.0 GA 最新演进,建议结合官方文档同步实践代码,特别注意版本迁移要点(如 artifactId 更名、Advisor 替换等),并在实际项目中遵循最佳实践规避常见陷阱。


