天天看點

Python擷取IAccessible接口

MSAA的全稱是Microsoft Active Accessibility。這是類似DCOM技術。技術模型是這樣的,UI程式可以暴露出一個Interface,友善另一個程式對其進行控制。 MSAA技術的初衷是為了友善殘障人士使用Windows 程式。比如盲人看不到視窗,但是盲人可以通過一個USB讀屏器連接配接到電腦上, 讀屏器通過UI程式暴露出來的這個Interface,就可以擷取程式資訊,通過盲文或者其它形式傳遞給盲人。

MSAA提供了如此友善的功能, UI自動化測試自然可以借用這項技術。MSAA暴露出來的Interface叫做 IAccessible。

Python中擷取IAccessible接口的方法如下:

<a></a>

from ctypes import windll, oledll, WinError, byref, POINTER

from ctypes.wintypes import POINT

from comtypes import COMError

from comtypes.automation import VARIANT

from comtypes.client import GetModule

# create wrapper for the oleacc.dll type library

GetModule("oleacc.dll")

# import the interface we need from the wrapper

from comtypes.gen.Accessibility import IAccessible

def AccessibleObjectFromPoint(x, y):

    "Return an accessible object and an index. See MSDN for details."

    pacc = POINTER(IAccessible)()

    var = VARIANT()

    oledll.oleacc.AccessibleObjectFromPoint(POINT(x, y), byref(pacc),

byref(var))

    return pacc, var

def AccessibleObjectFromWindow(hwnd):

    ptr = POINTER(IAccessible)()

    res = oledll.oleacc.AccessibleObjectFromWindow(

      hwnd,0,

      byref(IAccessible._iid_),byref(ptr))

    return ptr