Reading out Edge Browser Title & Url with System.Windows.Automation

后端 未结 1 792
无人及你
无人及你 2020-12-21 14:31

I\'m trying to read out the TITLE & URL from the Microsoft EDGE Browser. Doing this with System.Windows.Automation most preferably since the code base already uses this

相关标签:
1条回答
  • 2020-12-21 14:49

    You're almost there. You just need to get the TextPattern from the addressEditBox element. Here is a full sample Console app that dumps out all currently running Edge's windows on the desktop:

    class Program
    {
        static void Main(string[] args)
        {
            AutomationElement main = AutomationElement.FromHandle(GetDesktopWindow());
            foreach(AutomationElement child in main.FindAll(TreeScope.Children, PropertyCondition.TrueCondition))
            {
                AutomationElement window = GetEdgeCommandsWindow(child);
                if (window == null) // not edge
                    continue;
    
                Console.WriteLine("title:" + GetEdgeTitle(child));
                Console.WriteLine("url:" + GetEdgeUrl(window));
                Console.WriteLine();
            }
        }
    
        public static AutomationElement GetEdgeCommandsWindow(AutomationElement edgeWindow)
        {
            return edgeWindow.FindFirst(TreeScope.Children, new AndCondition(
                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
                new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
        }
    
        public static string GetEdgeUrl(AutomationElement edgeCommandsWindow)
        {
            var adressEditBox = edgeCommandsWindow.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"));
    
            return ((TextPattern)adressEditBox.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
        }
    
        public static string GetEdgeTitle(AutomationElement edgeWindow)
        {
            var adressEditBox = edgeWindow.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"));
    
            return adressEditBox.Current.Name;
        }
    
        [DllImport("user32")]
        public static extern IntPtr GetDesktopWindow();
    }
    
    0 讨论(0)
提交回复
热议问题