Microsoft Edge: Get Window URL and Title

后端 未结 7 1086
滥情空心
滥情空心 2020-12-06 15:33

Previously I was using ShellWindows() API for IE to get the window Title and URL for my application Now with new development, Microsoft Edge is new and has many features und

7条回答
  •  有刺的猬
    2020-12-06 15:51

    I'm not familiar with what's possible through Shell APIs here, but I just tried a test with UIA. I wrote the code below to access the title, url and window handle, and it seemed to work ok. I ran the code while Edge had a few tabs showing, and the page presented was Bing.com. This is the data found by the test...

    The C# code uses a UIA interop dll that I generated through the tlbimp tool.

    The test code makes a few assumptions which might need tightening up, but overall it looks like it can get the data you need. If you find this code doesn't work for you, if you let me know exactly which elements are involved, I can look into it.

    Thanks,

    Guy

    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);
    }
    

提交回复
热议问题