标签:uia uiautomation ui自动化 文本框
问题:获取Text控件的内容
场景:Win+R启动运行框,需要设置或获取Edit控件中的内容
解决:
1.使用.NETFramework
UIA中有个TextPattern,里面有TextPatternRange,即可得到Text的内容
代码如下:
var desktop = AutomationElement.RootElement; var condition_Name = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window); // 定义我们的查找条件,名字是test var condition_Class = new PropertyCondition(AutomationElement.NameProperty, "运行"); // 定义我们的查找条件,名字是test var window = desktop.FindFirst(TreeScope.Subtree, new AndCondition(condition_Class, condition_Name)); // 在桌面的子控件中查找第一个符合条件的窗体。 var condition_Edit = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit); var textedit = window.FindFirst(TreeScope.Subtree, condition_Edit); Console.WriteLine(textedit.Current.ProcessId); Console.WriteLine(textedit.Current.ClassName); Console.WriteLine(textedit.Current.NativeWindowHandle); var clickPattern = (TextPattern)textedit.GetCurrentPattern(TextPattern.Pattern); Console.WriteLine(clickPattern.DocumentRange.GetText(100)); Console.WriteLine(clickPattern.DocumentRange.GetText(100));
按道理,com接口应该与.NET Framework一致,即先获取TextPattern,然后获取TextPatternRange,获取Text,但是通过com接口获取到的TextPattern为Null
com的过程应该是:获取ValuePattern,使用SetValue或CurrentValue,设置或获取值
代码如下:
#获取文本框内容 desktop = AutomationElement.GetRootElement() controlTypeProperty = PropertyCondition(UIAPropertyIds.UIA_ControlTypePropertyId, UIAControlTypeIds.UIA_WindowControlTypeId) nameProperty = PropertyCondition(UIAPropertyIds.UIA_NamePropertyId, u"运行") window = desktop.FindFirst(TreeScope.TreeScope_Descendants, AndCondition(controlTypeProperty, nameProperty)) print window.GetWindowPattern() subControlTypeProperty = PropertyCondition(UIAPropertyIds.UIA_ControlTypePropertyId, UIAControlTypeIds.UIA_EditControlTypeId) textedit = window.FindFirst(TreeScope.TreeScope_Descendants, subControlTypeProperty) print textedit.element print textedit.Current.GetProcessId() print textedit.Current.GetClassName() print hex(textedit.Current.GetNativeWindowHandle()) buttonProperty = PropertyCondition(UIAPropertyIds.UIA_NamePropertyId, u"确定") button = window.FindFirst(TreeScope.TreeScope_Descendants, buttonProperty) print textedit.GetValuePattern().CurrentValue
标签:uia uiautomation ui自动化 文本框
原文地址:http://blog.csdn.net/sogouauto/article/details/43405321