Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

.net - Microsoft Edge: Get Window URL and Title

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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

MessageBox showing page title, url and Edge window handle

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 +
        "
URL: " + url + 
        "
hwnd: " + edgeWindowHandle);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...