Microsoft Edge:获取窗口 URL 和标题

Waq*_*qas 3 .net microsoft-edge

以前我使用 ShellWindows() API for IE 来获取我的应用程序的窗口标题和 URL 现在随着新的开发,Microsoft Edge 是新的,并且有许多正在开发的功能。

我想知道如何获取在 MS-Edge 中打开的所有页面的 URL 和标题。由于没有任何外壳 API 与 MS-Edge 一起使用。我也尝试过 UI 自动化,但它没有返回所有的 UI 元素

我正在使用 MS Visual Studio 2010 进行开发。我需要新版本的 Visual Studio 吗?有人可以帮助我了解如何访问标题和 URL 吗?或者由于安全原因,MS-Edge 是否不允许这样的访问?谢谢

小智 5

我不熟悉此处通过 Shell API 可能实现的功能,但我刚刚尝试使用 UIA 进行测试。我写了下面的代码来访问标题、网址和窗口句柄,它似乎工作正常。当 Edge 显示几个选项卡时,我运行了代码,显示的页面是 Bing.com。这是测试发现的数据...

MessageBox 显示页面标题、url 和 Edge 窗口句柄

C# 代码使用我通过 tlbimp 工具生成的 UIA 互操作 dll。

测试代码做出了一些可能需要加强的假设,但总的来说,它看起来可以获得您需要的数据。如果您发现此代码对您不起作用,如果您让我确切知道涉及哪些元素,我可以调查一下。

谢谢,

盖伊

IUIAutomationElement rootElement = uiAutomation.GetRootElement();

int propertyName = 30005; // UIA_NamePropertyId
int propertyAutomationId = 30011; // UIA_AutomationIdPropertyId
int propertyClassName = 30012; // UIA_ClassNamePropertyId
int propertyNativeWindowHandle = 30020; // UIA_NativeWindowHandlePropertyId

// Get the main Edge element, which is a direct child of the UIA root element.
// For this test, assume that the Edge element is the only element with an
// AutomationId of "TitleBar".
string edgeAutomationId = "TitleBar";

IUIAutomationCondition condition = 
    uiAutomation.CreatePropertyCondition(
        propertyAutomationId, edgeAutomationId);

// Have the window handle cached when we find the main Edge element.
IUIAutomationCacheRequest cacheRequestNativeWindowHandle = uiAutomation.CreateCacheRequest();
cacheRequestNativeWindowHandle.AddProperty(propertyNativeWindowHandle);

IUIAutomationElement edgeElement = 
    rootElement.FindFirstBuildCache(
        TreeScope.TreeScope_Children, 
        condition,
        cacheRequestNativeWindowHandle);

if (edgeElement != null)
{
    IntPtr edgeWindowHandle = edgeElement.CachedNativeWindowHandle;

    // Next find the element whose name is the url of the loaded page. And have
    // the name of the element related to the url cached when we find the element.
    IUIAutomationCacheRequest cacheRequest =
        uiAutomation.CreateCacheRequest();
    cacheRequest.AddProperty(propertyName);

    // For this test, assume that the element with the url is the first descendant element
    // with a ClassName of "Internet Explorer_Server".
    string urlElementClassName = "Internet Explorer_Server";

    IUIAutomationCondition conditionUrl =
        uiAutomation.CreatePropertyCondition(
            propertyClassName,
            urlElementClassName);

    IUIAutomationElement urlElement =
        edgeElement.FindFirstBuildCache(
            TreeScope.TreeScope_Descendants,
            conditionUrl,
            cacheRequest);

    string url = urlElement.CachedName;

    // Next find the title of the loaded page. First find the list of 
    // tabs shown at the top of Edge.
    string tabsListAutomationId = "TabsList";

    IUIAutomationCondition conditionTabsList =
        uiAutomation.CreatePropertyCondition(
            propertyAutomationId, tabsListAutomationId);

    IUIAutomationElement tabsListElement =
        edgeElement.FindFirst(
            TreeScope.TreeScope_Descendants,
            conditionTabsList);

    // Find which of those tabs is selected. (It should be possible to 
    // cache the Selection pattern with the above call, and that would
    // avoid one cross-process call here.)
    int selectionPatternId = 10001; // UIA_SelectionPatternId
    IUIAutomationSelectionPattern selectionPattern = 
        tabsListElement.GetCurrentPattern(selectionPatternId);

    // For this test, assume there's always one selected item in the list.
    IUIAutomationElementArray elementArray = selectionPattern.GetCurrentSelection();
    string title = elementArray.GetElement(0).CurrentName;

    // Now show the title, url and window handle.
    MessageBox.Show(
        "Page title: " + title +
        "\r\nURL: " + url + 
        "\r\nhwnd: " + edgeWindowHandle);
}
Run Code Online (Sandbox Code Playgroud)