【问题标题】:Windows UIAutomation is not detecting window elementWindows UIAutomation 未检测到窗口元素
【发布时间】:2020-06-08 20:27:14
【问题描述】:

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

当我使用 UI Spy 检查时,它会显示带有类名和进程 ID 的元素。我正在寻找的元素是窗口元素,它属于资源管理器进程。因此,当我尝试使用以下代码时,它返回 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);
                }
             }
       }

【问题讨论】:

  • 当自动化暴露RootElement 属性时,您为什么要摆弄进程、主窗口等:“获取当前桌面的根 AutomationElement。”?
  • @Damien_The_Unbeliever 非常感谢。使用了根元素属性,它帮助我找到了窗口元素。

标签: c# ui-automation


【解决方案1】:

正如@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。

【讨论】:

    猜你喜欢
    • 2010-10-21
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    相关资源
    最近更新 更多