前言: 此问题是在Python开发环境下关于UIAutomation的报错问题,问题来源于生产环境中的几台WIN10系统报错。
module UIAutomationClient' has no attribute 'IUIAutomation'
Can not load UIAutomationCore.dll.
1, You may need to install Windows Update KB971513 if your OS is Windows XP, see
2, you need to use an UIAutomationInitializerInThread object if use uiautomation in a thread, see demos/uiautomation_in_thread.py
>cd C:WindowsSystem32
>regsvr32 UIAutomationCore.dll
Traceback (most recent call last):File "test_hook.py", line 112, in get_mouseFile "uiautomationuiautomation.py", line 8164, in ControlFromPointFile "uiautomationuiautomation.py", line 52, in instanceFile "uiautomationuiautomation.py", line 71, in __init__File "uiautomationuiautomation.py", line 60, in __init__
AttributeError: module UIAutomationClient' has no attribute 'IUIAutomation'
class _AutomationClient:_instance = None@classmethoddef instance(cls) -> '_AutomationClient':"""Singleton instance (this prevents com creation on import)."""if cls._instance is None:cls._instance = cls()return cls._instancedef __init__(self):tryCount = 3for retry in range(tryCount):try:self.UIAutomationCore = comtypes.client.GetModule("UIAutomationCore.dll")self.IUIAutomation = comtypes.client.CreateObject("{ff48dba4-60ef-4201-aa87-54103eef594e}", interface=self.UIAutomationCore.IUIAutomation)self.ViewWalker = self.IUIAutomation.RawViewWalker#self.ViewWalker = self.IUIAutomation.ControlViewWalkerbreakexcept Exception as ex:if retry + 1 == tryCount:Logger.WriteLine('''
{}
Can not load UIAutomationCore.dll.
1, You may need to install Windows Update KB971513 if your OS is Windows XP, see
2, you need to use an UIAutomationInitializerInThread object if use uiautomation in a thread, see demos/uiautomation_in_thread.py'''.format(ex), ConsoleColor.Yellow)raise ex
>>> import comtypes.client
>>> core = comtypes.client.GetModule("UIAutomationCore.dll")
>>> core
<module _944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'D:\DevelopTools\Python\Python37\lib\site-packages\comtypes\gen\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>
<module _944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'C:\Users\Ow\AppData\Local\Temp\comtypes_cache\main-37\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>
<module _944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'C:\Users\Ow\AppData\Local\Temp\comtypes_cache\main-37\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>
<module _944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'C:\Users\Ow\AppData\Local\Temp\comtypes_cache\main-37\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>
符合源码逻辑,循环了三次。
重点是,对比验证有了结果!两者加载的UIAutomationCore.dll确实不是同一处,然后找到本地目录C:UsersOwAppDataLocalTempcomtypes_cachemain-37后发现,其中的_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py文件为空,对比python3库中安装的comtypes源码,不难发现,正是因为该文件中缺少IUIAutomation类,才报错AttributeError: module ‘UIAutomationClient’ has no attribute ‘IUIAutomation’
既然知道出错的原因,那么就容易对症下药,根治问题。要么将comtypes_cache目录整个删掉,要么将UIAutomationClient import IUIAutomation手动添加到项目源码中去。
至此,项目问题已解决。但是还有一系列值得思考的问题,就是为何会产生comtypes_cache目录?又为啥项目软件加载的UIAutomationCore.dll会优先找到本地临时目录中?看了comtypes_cache目录的生成日期就是昨天,是因何事件,临时目录中会写入comtypes_cache?
暂时未知,有时间可以研究一下。
未完,待续。。。
本文发布于:2024-02-01 20:04:01,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170678903939114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |