【发布时间】:2016-10-25 19:49:24
【问题描述】:
我正在使用 Microsoft 的 UIAutomation 库用于 C#。我有一个具有以下属性的元素:
- LocalizedControlType:“编辑”
- 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