【问题标题】:Reading out Edge Browser Title & Url with System.Windows.Automation使用 System.Windows.Automation 读出 Edge 浏览器标题和 URL
【发布时间】:2015-11-19 05:08:29
【问题描述】:

我正在尝试从 Microsoft EDGE 浏览器中读取 TITLE 和 URL。 最好使用 System.Windows.Automation 执行此操作,因为代码库已经将其用于其他问题。

  1. System.Windows.Automation 可以吗?
  2. 如何访问网址?

我目前已经这么远了:

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

【问题讨论】:

    标签: c# ui-automation microsoft-edge microsoft-ui-automation


    【解决方案1】:

    你快到了。您只需要从 addressEditBox 元素中获取TextPattern。这是一个完整的控制台应用程序示例,它会在桌面上转储所有当前正在运行的 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();
    }
    

    【讨论】:

    • 谢谢,会尽快试用!我猜我无法理解 System.Windows.Automation API :)...谢谢。
    • 此方法仅在 EDGE 窗口最大化时有效。如果 EDGE 最小化,您能否建议如何使其工作?
    • 因为 UIA 最小化就看不到边缘
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 2023-01-19
    • 2015-09-13
    相关资源
    最近更新 更多