简体   繁体   中英

I am not able to capture Microsoft edge url from c# windows forms application

I have tried the following method to get the URL, but it is only working for windows default Edge browser, not working in updated version of Edge Browser(Ver: 83.0). I'm using this browser: https://www.microsoft.com/en-us/edge

public static string GetEdgeUrl(Process process)
{
            try
            {
                if (process == null)
                    throw new ArgumentNullException("process");
                if (process.MainWindowHandle == IntPtr.Zero)
                    return null;
                AutomationElement main = AutomationElement.FromHandle(process.MainWindowHandle);
                if (main == null) // not edge
                    return null;
                AutomationElement window = main.FindFirst(TreeScope.Children, new AndCondition(
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
            new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
                if (window == null) // not edge
                    return null;
                var adressEditBox = window.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"));
            return ((TextPattern)adressEditBox.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Finally I found the answer. This is working for me. I used the same technique like Chrome. Thank you @stuartd

if (process == null)
                    throw new ArgumentNullException("process");
                if (process.MainWindowHandle == IntPtr.Zero)
                    return null;
                AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
                if (element == null)
                    return null;
                AutomationElement edit = element.FindFirst(TreeScope.Subtree,
                     new AndCondition(
                          new PropertyCondition(AutomationElement.NameProperty, "address and search bar", PropertyConditionFlags.IgnoreCase),
                          new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
                if (edit != null)
                {
                    var i = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
                    return i;
                }
                else
                {
                    return "";
                }

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