2

Я пытаюсь прочитать URL-адрес TITLE & из браузера Microsoft EDGE. Выполнение этого с помощью System.Windows.Automation наиболее предпочтительно, поскольку база кода уже использует это для других проблем.Чтение Edge Browser Title & Url with System.Windows.Automation

  1. Возможно ли использование системы.Windows.Automation?
  2. Как получить доступ к URL-адресу?

Я в настоящее время это далеко:

AutomationId "TitleBar" 
ClassName "ApplicationFrameWindow" 
Name = [string] 
=> Reading out this element gives me the TITLE 

=> Walking it's 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. 

В коде:

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; 
         } 
        } 
       } 
      } 
      } 
     } 

ответ

4

Вы почти там. Вам просто нужно получить TextPattern от элемента addressEditBox. Вот полное приложение консоли Console, которое выгружает все текущие окна Edge на рабочем столе:

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

Спасибо, попробуем это как можно скорее! Я не понял API System.Windows.Automation, я думаю:) ... Спасибо. –

+0

Этот метод работает только при максимальном увеличении окна EDGE. Можете ли вы предложить, как заставить его работать, если EDGE сведен к минимуму? –

+0

Поскольку МАУ не видит края, если оно минимизировано –

Смежные вопросы