AutomationElement desktop = AutomationElement.RootElement; var calcFrame1 = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "CalcFrame")); ClickCalcButton(calcFrame1, "3"); ClickCalcButton(calcFrame1, "6"); ClickCalcButton(calcFrame1, "5"); ClickCalcButton(calcFrame1, "*"); ClickCalcButton(calcFrame1, "1"); ClickCalcButton(calcFrame1, "2"); ClickCalcButton(calcFrame1, "=");
其中ClickCalcButton是我封装的一个方法:
private static void InvokeButton(AutomationElement e) { InvokePattern invoke = (InvokePattern)e.GetCurrentPattern(InvokePattern.Pattern); invoke.Invoke(); } private static void ClickCalcButton(AutomationElement calcFrame1, string name) { Condition conditionBtnPlus = new AndCondition( new PropertyCondition(AutomationElement.ClassNameProperty, "Button"), new PropertyCondition(AutomationElement.NameProperty, name) ); var btn = calcFrame1.FindFirst(TreeScope.Descendants, conditionBtnPlus); if (btn == null) { throw new Exception("找不到名字为"+name+"的计算器按钮"); } InvokeButton(btn); }
文章篇幅有限,特别是对于一些没有Win32基础的朋友,光看上面的文字会不太容易懂,因此我录制了一套大约90分的视频教程,详细的讲解了uiautomation的使用,希望能够帮到大家。
视频教程地址如下
以上就是关于uiautomation的全部内容,相信你一定会非常满意。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-5733-2.html
同志们是取向差异