0

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;
        }
    }
  • Edge is just rebranded Chrome now, so you can maybe use the same technique as shown [here](https://stackoverflow.com/a/52995963/43846) – stuartd Jun 19 '20 at 14:17
  • You can just search for a descendant of the Main Window (`Chrome_Widget_1`, or depending on the starting position in the TreeScope, `BrowserRootView`) with class name `OmniboxViewViews`. It's a TextBox type control, so you need to get its Value (ValuePattern). – Jimi Jun 19 '20 at 14:32
  • 1
    @stuartd I used the same code like Chrome it's worked for me. Thank you so much. Really I was stuck too many days. – Tony Walter Jun 19 '20 at 18:10
  • @Jimi I can't get you, if possible please change my code and post it – Tony Walter Jun 19 '20 at 18:16
  • I don't know what there is to get. You have the handle of the main Window, use AutomationElement.FromHandle() to get the UI Element of it, setup an AndCondition with `AutomationElement.ControlTypeProperty, ControlType.Edit` and `AutomationElement.ClassNameProperty, "OmniboxViewViews"`, use `var urlEditControl = [MainWindow].FindFirst(TreeScope.Descendants, [Your AndCondition]);`. If `urlEditControl` is not null, `string urlText = (urlEditControl.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern).Current.Value;`. That's all. – Jimi Jun 19 '20 at 18:54
  • @TonyWalter, from your previous comment. It looks like your issue is resolved now. I suggest you share your solution as an answer to this question. It can help other community members in the future in similar kinds of issues. Thanks for your understanding. – Deepak-MSFT Jun 22 '20 at 01:28
  • @Deepak-MSFT yes I'll add the answer – Tony Walter Jun 22 '20 at 19:25

1 Answers1

0

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 "";
                }
  • Thanks for adding the answer. you can try to accept your own answer. You can click on the check icon to accept the answer. See here: https://imgur.com/a/yxZ7us8 Thanks for your understanding. – Deepak-MSFT Jun 23 '20 at 03:25