several answered questions about this on stackoverflow,但它们似乎已过时并且不再起作用。 Chrome浏览器具有changed its structure entirely。如果我尝试AccessibleObjectFromEvent技术,那么我只会获得accName和accValue的NULL值。似乎there are solutions for python,但是我找不到C++的任何解决方案。如何在C++中检索 Activity 的Tab URL?

最佳答案

使用Window SDK中的Inspect工具,我们可以获得Chrome的URL编辑框的属性名称:

Name:           "Address and search bar" *
ControlType:    UIA_EditControlTypeId (0xC354)

要在Chrome中找到 Activity 标签,请使用FindWindowEx在桌面中找到第一个可见的Chrome子窗口。

然后使用UI Automation查找具有该ID的编辑控件。或者只是在Chrome中找到第一个编辑控件。

下面的示例使用ATL COM类,它需要Visual Studio
#define UNICODE
#include <Windows.h>
#include <AtlBase.h>
#include <AtlCom.h>
#include <UIAutomation.h>

int main()
{
    CoInitialize(NULL);
    HWND hwnd = NULL;
    while(true)
    {
        hwnd = FindWindowEx(0, hwnd, L"Chrome_WidgetWin_1", NULL);
        if(!hwnd)
            break;
        if(!IsWindowVisible(hwnd))
            continue;

        CComQIPtr<IUIAutomation> uia;
        if(FAILED(uia.CoCreateInstance(CLSID_CUIAutomation)) || !uia)
            break;

        CComPtr<IUIAutomationElement> root;
        if(FAILED(uia->ElementFromHandle(hwnd, &root)) || !root)
            break;

        CComPtr<IUIAutomationCondition> condition;

        //URL's id is 0xC354, or use UIA_EditControlTypeId for 1st edit box
        uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
                CComVariant(0xC354), &condition);

        //or use edit control's name instead
        //uia->CreatePropertyCondition(UIA_NamePropertyId,
        //      CComVariant(L"Address and search bar"), &condition);

        CComPtr<IUIAutomationElement> edit;
        if(FAILED(root->FindFirst(TreeScope_Descendants, condition, &edit))
            || !edit)
            continue; //maybe we don't have the right tab, continue...

        CComVariant url;
        edit->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &url);
        MessageBox(0, url.bstrVal, 0, 0);
        break;
    }
    CoUninitialize();
    return 0;
}

对于非英语系统,"Address and search bar"可以使用其他名称

09-30 17:02
查看更多