Python获取IAccessible接口

MSAA的全称是Microsoft Active Accessibility。这是类似DCOM技术。技术模型是这样的,UI程序可以暴露出一个Interface,方便另一个程序对其进行控制。 MSAA技术的初衷是为了方便残疾人使用Windows 程序。比如盲人看不到窗口,但是盲人可以通过一个USB读屏器连接到电脑上, 读屏器通过UI程序暴露出来的这个Interface,就可以获取程序信息,通过盲文或者其它形式传递给盲人。

MSAA提供了如此方便的功能, UI自动化测试自然可以借用这项技术。MSAA暴露出来的Interface叫做 IAccessible。

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

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


posted @ 2010-06-05 10:45  CoderZh  阅读(4468)  评论(3编辑  收藏  举报