将IAccessible与Google Chrome一起使用会返回不完整的树

时间:2014-08-17 16:38:26

标签: c++ google-chrome winapi iaccessible

我正在尝试为Google Chrome创建质量检查自动化,以模拟点击次数并在标签按钮上接收点击事件。

我使用IAccessible interfaceAccessibleChildren API来获取完整的可访问元素树。

使用AccExplorer 2.0查看树时 - 它看起来很棒(见最后的图片)。

但我的程序只显示部分树,名称与我在AccExplorer上看到的名称不匹配。

我正在跑步:

  • Chrome 37 beta
  • Win 7 64位

为什么我无法查看整个树的任何想法?

由于

这是我的程序源码(C ++): (similar to MSDN example)

int _tmain(int argc, _TCHAR* argv[])
{
    int i=0;
    HWND hWndChrome = NULL;
    hWndChrome = (HWND)0x000702c0;
    wcout << L"\n\nChrome_WidgetWin_1 = "<< hex << hWndChrome << "\n--------------------------";

    CComPtr<IAccessible> pAccMain;
    ::AccessibleObjectFromWindow(hWndChrome, OBJID_CLIENT, IID_IAccessible, (void**)(&pAccMain));

    WalkTreeWithAccessibleChildren(pAccMain, 0);
    _getch();

    return 0;
}

HRESULT WalkTreeWithAccessibleChildren(__in CComPtr<IAccessible> pAcc, __in int depth)
{
    long childCount = 0;
    long returnCount = 0;

    HRESULT hr = pAcc->get_accChildCount(&childCount);

    if (childCount == 0)
        return S_FALSE;

    CComVariant* pArray = new CComVariant[childCount];
    hr = ::AccessibleChildren(pAcc, 0L, childCount, pArray, &returnCount);
    if (FAILED(hr))
        return hr;

    // Iterate through children.
    for (int x = 0; x < returnCount; x++)
    {
        CComVariant vtChild = pArray[x];
        // If it's an accessible object, get the IAccessible, and recurse.
        if (vtChild.vt == VT_DISPATCH)
        {
            CComPtr<IDispatch> pDisp = vtChild.pdispVal;
            CComQIPtr<IAccessible> pAccChild = pDisp;
            if (!pAccChild)
                continue;

            // Print current accessible element
            wcout << endl;
            for (int y = 0; y < depth; y++) wcout << L"    ";
            wcout << L"* " << GetName(pAccChild, CHILDID_SELF).data() << L"  |  " << GetRole(pAccChild, CHILDID_SELF).data() << L" (Object)";

            WalkTreeWithAccessibleChildren(pAccChild, depth + 1);       
        }
        // Else it's a child element so we have to call accNavigate on the parent,
        //   and we do not recurse because child elements can't have children.
        else
        {
            // Print current accessible element
            wcout << endl;
            for (int y = 0; y < depth; y++) wcout << L"    ";
            wcout << L"* " << GetName(pAcc, vtChild.lVal).data() << L"  |  " << GetRole(pAcc, vtChild.lVal).data() << " (Child)";
        }
    }
    delete[] pArray;
    return S_OK;
}

wstring GetName(__in CComPtr<IAccessible> pAcc, __in CComVariant varChild)
{
    if (!pAcc)
        return L"";
    CComBSTR bstrName;
    HRESULT hr = pAcc->get_accName(varChild, &bstrName);
    if (FAILED(hr))
        return L"";
    if (!bstrName.m_str)
        return L"<NULL>";
    return bstrName.m_str;
}

wstring GetRole(__in CComPtr<IAccessible> pAcc, __in CComVariant varChild)
{
    if (!pAcc)
        return L"";
    CComVariant varRoleID;
    HRESULT hr = pAcc->get_accRole(varChild, &varRoleID);
    if (FAILED(hr))
        return L"";
    WCHAR sRoleBuff[1024] = {0};
    hr = ::GetRoleText(varRoleID.lVal, sRoleBuff, 1024);
    if (FAILED(hr))
        return L"";
    return sRoleBuff;
}

这是控制台输出:

    Chrome_WidgetWin_1 = 000702C0
    --------------------------
    * Chrome Legacy Window  |  window (Object)
        * System  |  menu bar (Object)
        * <NULL>  |  title bar (Object)
            * IME  |  push button (Child)
            * Minimize  |  push button (Child)
            * Maximize  |  push button (Child)
            * Context help  |  push button (Child)
            * Close  |  push button (Child)
        * Application  |  menu bar (Object)
        * Chrome Legacy Window  |  client (Object)
    //  -----> { there should be a big sub-tree here } <----- //
        * Vertical  |  scroll bar (Object)
            * Line up  |  push button (Child)
            * Page up  |  push button (Child)
            * Position  |  indicator (Child)
            * Page down  |  push button (Child)
            * Line down  |  push button (Child)
        * Horizontal  |  scroll bar (Object)
            * Column left  |  push button (Child)
            * Page left  |  push button (Child)
            * Position  |  indicator (Child)
            * Page right  |  push button (Child)
            * Column right  |  push button (Child)
        * <NULL>  |  grip (Object)

这是一个显示完整树的AccExplorer屏幕截图:(某些节点已折叠) AccExplorer working example


修改
当使用 OBJID_WINDOW 而不是 OBJID_WINDOW 时(如示例中的 AccessibleObjectFromWindow ) - 我得到一个包含更多节点的树,但我仍然可以&#39 ; t看到标签元素。

1 个答案:

答案 0 :(得分:3)

好的,所以经过不久之后,我发现了一件让小事变得微不足道的小事...... 我忘了使用 CoInitialize() ...

有趣的是,没有 CoInitialize()它可以工作,但只有部分,并且没有HRESULT表明这一点(没有像CO_E_NOTINITIALIZED那样得到任何东西)。

无论如何 - 如果您使用 IAccessible 并且 CoInitialize - 期望事情无法正常工作......