识别UI元素
找到窗口就可以开始找窗口上的UI元素了。
比如我想找Calculator上的文本框
可以用如下代码实现:
…
//找到Desktop
AutomationElement Desktop = AutomationElement.RootElement;
//找到Calculator窗口
AutomationElement CalcWindows = Desktop.FindFirst(TreeScope.Children,
new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
new PropertyCondition(AutomationElement.ClassNameProperty, “SciCalc”)
)
);
//找到Calculator的Edit控件
AutomationElement CalcEdit=CalcWindows.FindFirst(TreeScope.Children,
new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
new PropertyCondition(AutomationElement.ClassNameProperty, “Edit”)
)
);
AutomationElement.FindFirst
的方法
public AutomationElement FindFirst(
TreeScope scope,
Condition condition
)
TreeScope是个枚举类型
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Children和Descendants比较常用。Subtree也用比较多。
Condition 类
在 UI 自动化目录树中搜索元素时应用于筛选的条件的基类型。
主要都使用下面几个子类
System.Windows.Automation.AndCondition
System.Windows.Automation.NotCondition
System.Windows.Automation.OrCondition
System.Windows.Automation.PropertyCondition
AndCondition 表示一个与(And)条件
NotCondition 表示一个非(Not)条件OrCondition 表示一个或(Or)条件
PropertyCondition 它测试属性是否具有指定的值
可以在UISpy的右边Identification中找到相应的值。
一般用ControlType,ClassName, Automationid 和 Name 就够用了。其他的一般较少使用。