微软边缘:获取窗口的URL和标题 [英] Microsoft Edge: Get Window URL and Title

查看:583
本文介绍了微软边缘:获取窗口的URL和标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

previously我使用ShellWindows()API的IE浏览器,以获取窗口标题和URL我的应用程序 现在有了新的发展,微软Edge是新的和正在开发的许多功能。

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 under development.

我想知道我怎么可以得到所有的网页的URL,标题在MS-边打开。 由于没有壳的API正在使用MS-边缘。 我想也与UI自动化,但它并不会返回所有的UI元素

I want to know how I can get the URL and Title of all the pages opened in MS-Edge. As none of the shell APIs are working with MS-Edge. I tried also with UI Automation but it does not return all the UI elements

我使用微软的Visual Studio 2010中进行开发。我需要的Visual Studio的新版本吗? 任何人可以帮助我如何访问标题和URL? 还是MS-边缘不允许出于安全这样的访问? 谢谢

I am using MS Visual Studio 2010 for development. Do I need new version of Visual Studio? Can anybody help me about how to access the Title and URL? Or Does MS-Edge not allow such an access due to security? thanks

推荐答案

我不熟悉什么是可以通过外壳的API在这里,但我只是想用UIA测试​​。我写的code以下访问标题,URL和窗口句柄,它似乎工作确定。我跑了code,而边缘有一些标签,显示,并在页面presented是Bing.com。这是由测试中找到的数据...

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...

消息框显示网页标题,URL和边缘窗口句柄

在C#code使用我通过TLBIMP工具生成一个UIA互操作的dll。

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

测试code,使这可能需要收紧了几个假设,但整体看起来它可以得到你需要的数据。如果您发现该code你不工作,如果你让我知道到底元素都参与其中,我可以看看吧。

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.

谢谢

盖伊

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

这篇关于微软边缘:获取窗口的URL和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆