Windows UIAutomation 未检测到 window 元素

Windows UIAutomation is not detecting window element

我正在尝试使用 UIAutomation 访问文件夹,但未检测到 window 元素。

当我检查 UI Spy 时,它显示了具有 class 名称和进程 ID 的元素。我正在寻找的元素是 window 元素,它属于资源管理器进程。因此,当我尝试使用以下代码时,它 returns 0 个元素。我附上图片以供参考。请帮助我。

Process[] windowFolders = Process.GetProcessesByName("explorer");
        foreach (Process proc in windowFolders)
        {
            Console.WriteLine(proc.GetType());
            proc.Refresh();
            Console.WriteLine(proc.MainWindowHandle);
            if (proc.MainWindowHandle.ToInt32() != 0)
            {
                AutomationElement windowExplorer = AutomationElement.FromHandle(proc.MainWindowHandle);
                AutomationElementCollection ewindows = windowExplorer.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.window));
                foreach (AutomationElement ewindow in ewindows)
                {
                    Console.WriteLine("Window Name: " + ewindow.Current.Name + " Window class name: " + ewindow.Current.ClassName);
                }
             }
       }

正如@Damien_The_Unbeliever 所建议的,我使用了根元素 属性。在 RootElement 属性 的帮助下找到了解决方案。在当前桌面上查找元素非常有用

以下是我自己找到的解决方案。

AutomationElementCollection desktopChildren = AutomationElement.RootElement.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Windows));
        foreach(AutomationElement dChil in desktopChildren)
        {
            if (dChil.Current.Name.Contains("Chipset Software"))
            {
                Console.WriteLine($"{MethodBase.GetCurrentMethod()}: Found Chipset_Software Window");
            }
       }

非常感谢@Damien_The_Unbeliever。