automationElement.RootElement.FindFirst 总是 returns 空

automationElement.RootElement.FindFirst always returns null

我有一个要求可能需要使用 UI 自动化之类的东西,所以我正在尝试学习基础知识,但运气不佳。

我在不同文章的几个示例中找到了这行代码:

var calcWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "Calculator"));

但是每当我 运行 这一行时,calcWindow 为空。我注意到当我 运行 2 个计算器实例时,它会被设置为一个值。

同样的问题也适用于 RootElement.FindAll,当计算器的单个实例 运行s 时它 returns null 但当我 运行 2 个实例时,它将return 一个值及其 .Count() 属性 设置为 1。

有什么想法吗?

谢谢。

更新 1

我可能应该包括我正在看的文章:

请注意,我运行在 Windows 10 Pro 64 位(os 构建:17134.112 - 版本:1803)上使用 .NET 2015 进行这些测试。

在 windows10 中,计算器是一个 UWP 应用程序,因此您需要使用不同的方法来找到它。

这里是 FlaUI 如何获得新计算器与旧计算器的示例。 FlaUI 是在托管 UIAv2 API 和非托管 UIAv3 COM 包装器之上编写的。

    protected override Application StartApplication()
    {
        if (OperatingSystem.IsWindows10())
        {
            // Use the store application on those systems
            return Application.LaunchStoreApp("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
        }
        if (OperatingSystem.IsWindowsServer2016())
        {
            // The calc.exe on this system is just a stub which launches win32calc.exe
            return Application.Launch("win32calc.exe");
        }
        return Application.Launch("calc.exe");
    }