You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pywin32操作CANoe COM接口设置Enabled属性报KeyError问题求助

关于Python通过COM接口操作CANoe TestUnit属性的问题

我正尝试通过COM接口实现CANoe的自动化操作,但遇到了无法解决的问题:

可正常执行的代码

以下代码能正确获取TestUnit的Name字符串属性和Enabled布尔属性:

app = DispatchEx('CANoe.Application')
configs = app.Configuration.TestConfigurations
config = configs.Item(1)
unit = config.TestUnits.Item(1)
unit.Name
unit.Enabled

设置属性时的报错

但当尝试设置Enabled属性为True或False时,会触发如下报错:

>>> unit.Enabled = False
Traceback (most recent call last):
  File "C:\Python\Python310\lib\site-packages\win32com\client\__init__.py", line 590, in __setattr__
    args, defArgs = self._prop_map_put_[attr]
KeyError: 'Enabled'

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python\Python310\lib\site-packages\win32com\client\__init__.py", line 592, in __setattr__
        raise AttributeError(
    AttributeError: '<win32com.gen_py.CANoe 12.0 Type Library.ITestUnit instance at 0x1977650030784>' object has no attribute 'Enabled'

异常现象补充

  • 相同的设置操作在VBS中可正常执行(需将VBS脚本迁移至Python)
  • 查看unit对象的内部属性时,发现缺少部分属性(如Elements):
>>> print(dir(unit))
['Application', 'CLSID', 'Enabled', 'Name', 'Parent', 'Report', 'Verdict', '_ApplyTypes_', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_good_object_', '_get_good_single_object_', '_oleobj_', '_prop_map_get_', '_prop_map_put_', 'coclass_clsid']
>>>
  • 查看_prop_map_put_时发现其为空:
>>> print(unit._prop_map_put_)
{}
>>>

尝试过的方法

我尝试用setattr设置Enabled为True,虽未触发异常,但之后无法再调用unit.Enabled,似乎需要配合_ApplyTypes_操作。

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 22:55:38