【问题标题】:Unable to locate element using UIAutomation无法使用 UIAutomation 定位元素
【发布时间】:2016-10-25 19:49:24
【问题描述】:

我正在使用 Microsoft 的 UIAutomation 库用于 C#。我有一个具有以下属性的元素:

  • Lo​​calizedControlType:“编辑”
  • AutomationId:“数据库”
  • 只读:错误
  • 文本字段

我可以使用 Inspect 读取这些属性。但是,我无法在 C# 中使用以下命令找到它:

AutomationElement ae = root.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "Database"), new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "edit"));

事实上,我能找到它的唯一方法是通过以下命令(跟随树):

AutomationElement ae = root.FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                    .FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                    .FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                    .FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                    .FindAll(TreeScope.Children, Condition.TrueCondition)[2]
                    .FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                    .FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                    .FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                    .FindAll(TreeScope.Children, Condition.TrueCondition)[2]
                    .FindAll(TreeScope.Children, Condition.TrueCondition)[0];

不幸的是,当我尝试编辑该值时,我收到一条错误消息,指出所选元素是只读的。但是,该元素实际上不是只读的。此外,该元素没有应有的automationId。使用 UIAutomation 查看时,树中的所有元素都没有 AutomationId。

我试图自动化的应用程序是一个专有应用程序,这是我第一次遇到这个问题。我不确定是什么原因造成的,我可以调查什么?

编辑:作为更新,我能够使用来自 root.FindAll(TreeScope.Descendants, Condition.TrueCondition) 的索引成功定位所需的元素。

但是,我有以下代码:

        cachedElement = findAll[index];
        Console.Write("cachedelement : "); cachedElement.ToConsole();           
        return 0;

如果我只是让程序运行,它会在尝试将文本写入文本字段时失败。此外,它显示所选元素没有属性(在 cachedelement.ToConsole 期间)。

有趣的是,如果我在 return 0 上设置一个断点; (或使用 System.Diagnostics.Debugger.Break()),cachedElement.ToConsole() 将 LCT 输出为“编辑”(仍然没有自动化 ID),并将文本正确写入文本字段。为什么我觉得这很有趣,因为断点是在调用 ToConsole 之后设置的。它根本不应该对正在运行的代码产生影响。

【问题讨论】:

  • 事实证明,我从来没有弄清楚为什么该字段缺少automationId,但是看起来元素在编辑时被添加到树中。我编写了一个小函数,它从给定的已知索引开始,然后根据元素的本地化控件类型进行查找。目前看来这是一个很好的解决方法。

标签: c# ui-automation


【解决方案1】:

根据您的第二个代码示例,您要查找的元素嵌套在树的下方很远,因此第一个代码示例永远不会找到该元素,因为它只查看桌面的子项。如果要从根节点查找元素,则需要将TreeScope.Descendants 用于树范围。我不建议使用后代,因为它会很慢,而且很可能在从根元素中使用时会导致 stack overflow

在最坏的情况下,我使用根元素的子范围为我的窗口执行 FindFirst,然后我使用应用程序根的后代。这仅适用于具有琐碎用户界面的小型应用程序。如果您有一些复杂的东西,例如带有单元格的网格,我建议您使用 FindFirst 和子范围,一直到您正在寻找的元素,类似于您使用 find all 的方式。

如果您可以发布用于编辑元素的代码,我可以使用有关如何编辑特定于您的情况的控件的信息来更新此答案。一般来说,虽然您将如何编辑文本框控件,但可以像这样使用 ValuePattern

  public void SetValue(AutomationElement element, string text)
  {
        var valuePattern = (ValuePattern) element.GetCurrentPattern(ValuePattern.Pattern);
        valuePattern.SetValue(text);
  }

希望这会有所帮助。

【讨论】:

  • 我最终在编辑中将其设置为后代。我最终创建了一个解决方法,以基于基本索引进行搜索,然后通过本地化控制类型进行搜索,因为这是我唯一的选择(AutomationId 仅在 C# 的 UIAutomation 中丢失。我可以在 Inspect 中看到它)。所以现在,我的问题已经解决了,但我仍然很好奇为什么所有属性都不能正确显示。
猜你喜欢
  • 1970-01-01
  • 2021-02-11
  • 2021-08-19
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多