Python-UIAutomation-for-Windows icon indicating copy to clipboard operation
Python-UIAutomation-for-Windows copied to clipboard

UIAutomationInitializerInThread is not working

Open ughstudios opened this issue 3 years ago • 1 comments
trafficstars

def bring_window_to_front(window: HWND):
    with uiautomation.UIAutomationInitializerInThread(True):
        window_handle = uiautomation.ControlFromHandle(window)
        window_handle.SetFocus()

I have the following code and it does not work.

call InitializeUIAutomationInCurrentThread in <Thread(FocusWatcher.update, started daemon 30992)>, inited True

module 'comtypes.gen.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 https://github.com/yinkaisheng/WindowsUpdateKB971513ForIUIAutomation
2, you need to use an UIAutomationInitializerInThread object if use uiautomation in a thread, see demos/uiautomation_in_thread.py



It is failing to initialuze the object IUIAutomation

class _AutomationClient:
    _instance = None

    @classmethod
    def instance(cls) -> '_AutomationClient':
        """Singleton instance (this prevents com creation on import)."""
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

    def __init__(self):
        tryCount = 3
        for 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.ControlViewWalker
                break
            except Exception as ex:
                if retry + 1 == tryCount:
                    Logger.WriteLine('''
                self.IUIAutomation = comtypes.client.CreateObject("{ff48dba4-60ef-4201-aa87-54103eef594e}", interface=self.UIAutomationCore.IUIAutomation)

This line is failing: AttributeError("module 'comtypes.gen.UIAutomationClient' has no attribute 'IUIAutomation'")

ughstudios avatar Jun 17 '22 18:06 ughstudios

I was able to resolve this by simply calling:

pythoncom.CoInitialize() and pythoncom.CoUninistialize() manually without the context manager

ughstudios avatar Jun 17 '22 18:06 ughstudios