简体   繁体   中英

C# UIA Get Microsoft Edge URL

I am currently using UIA to get the URL from Chrome, Firefox and IE11 using the following method:

string processName = "";

if (browser.Equals(BrowserType.GOOGLE_CHROME))
    processName = "chrome";
else if (browser.Equals(BrowserType.FIREFOX))
    processName = "firefox";
else if (browser.Equals(BrowserType.INTERNET_EXPLORER))
    processName = "iexplore";
else if (browser.Equals(BrowserType.MICROSOFT_EDGE))
    processName = "MicrosoftEdgeCP";

foreach(Process process in Process.GetProcessesByName(processName))
{
    string url = GetURLFromProcess(process, browser);
    if (url == null)
        continue;

    return url;
}

...

private string GetURLFromProcess(Process process, BrowserType browser)
{
    if (process == null)
        throw new ArgumentNullException("process");

    if (process.MainWindowHandle == IntPtr.Zero)
        return null;

    AutomationElement elm = AutomationElement.FromHandle(process.MainWindowHandle);
    if (elm == null)
        return null;
    string nameProperty = "";

    if (browser.Equals(BrowserType.GOOGLE_CHROME))
        nameProperty = "Address and search bar";
    else if (browser.Equals(BrowserType.FIREFOX))
        nameProperty = "Search or enter address";
    else if (browser.Equals(BrowserType.INTERNET_EXPLORER))
        nameProperty = "Address and search using Bing";
    else if (browser.Equals(BrowserType.MICROSOFT_EDGE))
        nameProperty = "Search or enter web address";

    AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Subtree, new AndCondition(
        new PropertyCondition(AutomationElement.NameProperty, nameProperty, PropertyConditionFlags.IgnoreCase),
        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));

    if (elmUrlBar != null)
    {
        return ((ValuePattern)elmUrlBar.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
    }

    return null;
}

Using Inspect, I can see that the URL bar in Edge is identified by 'Search or enter web address'. However, although the above method is working for other web browsers, this doesn't seem to be the case with Edge.

'elmUrlBar' always ends up being null and doesn't get found.

This code works in the last version Firefox, Internet Explorer, MS Edge and Chrome. This method works faster, than find address bar by name. And this method doesn't depend on localization of system.

private string GetURLFromProcess(Process process, BrowserType browser)
{
    if (process == null)
        throw new ArgumentNullException("process");

    if (process.MainWindowHandle == IntPtr.Zero)
        return null;

    AutomationElement elm = AutomationElement.FromHandle(process.MainWindowHandle);
    if (elm == null)
        return null;
    string nameProperty = "";

    if (browser.Equals(BrowserType.GOOGLE_CHROME))
        {
            var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
            if (elm1 == null) { return null; } // not the right chrome.exe
            var elm2 = elm1.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
            var elm3 = elm2.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
            var elm4 = elm3.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
            var elm5 = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
            var elmUrlBar = elm5.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
            var url = ((ValuePattern)elmUrlBar.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
            return url;
        }
    else if (browser.Equals(BrowserType.FIREFOX))
        {
             AutomationElement elm2 = elm.FindFirst(TreeScope.Children, new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar),
                    new PropertyCondition(AutomationElement.IsInvokePatternAvailableProperty, false)));
            if (elm2 == null)
                return null;
            var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ComboBox));
            if (elm3 == null)
                return null;
            var elmUrlBar = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
            if (elmUrlBar != null)
            {
                var url = ((ValuePattern)elmUrlBar.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
                url;
            }
        }
    else if (browser.Equals(BrowserType.INTERNET_EXPLORER))
        {
             AutomationElement elm2 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "ReBarWindow32"));
             if (elm2 == null)
                 return null;
             AutomationElement elmUrlBar = elm2.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
             var url = ((ValuePattern)elmUrlBar.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
             return url;
        }
    else if (browser.Equals(BrowserType.MICROSOFT_EDGE))
        {
            var elm2 = elm.FindFirst(TreeScope.Children, new AndCondition(
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
            new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));

            var elmUrlBar = elm2.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));

            var url = ((TextPattern)elmUrlBar.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
            return url;
        }

    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM