c# - 使用 System.Windows.Automation 读出 Edge 浏览器标题和 URL

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

我正在尝试从 Microsoft EDGE 浏览器读出标题和 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;
                      }
                   }
                }
             }
          }
       }

最佳答案

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

关于c# - 使用 System.Windows.Automation 读出 Edge 浏览器标题和 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32204961/

相关文章:

cors - 为什么 CORS 请求在 Microsoft Edge 中失败,但在其他浏览器中有效?

css - Edge 浏览器忽略 CSS 变量

c# - 如何在 Visual Studio 2017 ASP.NET Core Web 应用程序中集成 Bootstrap 4?

c# - Async/Await 如何处理 "old"结果?

ios - 在设备上使用 UIAutomation 进行录制时,它卡在 'Starting Capture...' 上。在模拟器上运行良好

wpf - 白色 UI 自动化 : Get WPF DataGrid cell value?

java - Microsoft Edge : org. openqa.selenium.remote.SessionNotFoundException: null

c# - 为什么我要替换异常?

c# - Discord.NET Guild.DefaultChannel 不起作用

javascript - Jasmine 数据提供程序无法工作(jasmine_data_provider_1.using 不是函数)