Reading the Header and Header of an Edge Browser Using System.Windows.Automation

I am trying to read the TITLE and URL from the Microsoft EDGE browser. Doing this with System.Windows.Automation is most preferable since the code base already uses this for other problems.

  • Is this possible with System.Windows.Automation?
  • How to access the url?

Currently, I'm so far away:

AutomationId "TitleBar" ClassName "ApplicationFrameWindow" Name = [string] => Reading out this element gives me the TITLE => Walking it children, I find the item "addressEditBox": AutomationId "addressEditBox" ClassName "RichEditBox" Name "Search or enter web address" => I always get back the string "Search or enter web address" => This is the control where the url is in, though it isn't updated as the user goes to a website, it always returns a fixed string. 

In code:

  var digger1 = AutomationElement.FromHandle(process.MainWindowHandle).RootElement.FindAll(TreeScope.Children, Condition.TrueCondition); foreach(AutomationElement d1 in digger1 { if(d1.Current.ClassName.Equals("ApplicationFrameWindow")) { var digger2 = d1.FindAll(TreeScope.Children, Condition.TrueCondition); foreach(AutomationElement d2 in digger2) { if(d2.Current.ClassName.Equals("Windows.Ui.Core.CoreWindow")) { var digger3 = d2.FindAll(TreeScope.Children, Condition.TrueCondition); foreach(AutomationElement d3 in digger3) { if(d3.Current.AutomationId.Equals("addressEditBox")) { var url = d3.Current.Name; return url; } } } } } } 
+2
source share
1 answer

You are almost there. You just need to get the TextPattern from the addressEditBox element. Here is the full Console console application that unloads all current Edge 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(); } 
+4
source

All Articles