在搜索Name属性和ControlType属性时,TreeScope在Internet Explorer中查找元素时遇到问题。
mainElement是表示“Internet Explorer_Server。
的AutomationElement所有自动化元素都列在UISpy中的mainElement下。
public Auto.AutomationElement GetElementByNameAndControlType(Auto.AutomationElement mainElement,System.Windows.Automation.ControlType controlType,string propertyName)
{
Auto.AutomationElement target = null;
Auto.PropertyCondition typeCondition1 = new Auto.PropertyCondition (Auto.AutomationElement.ControlTypeProperty, controlType);
Auto.PropertyCondition typeCondition2 = new Auto.PropertyCondition(Auto.AutomationElement.NameProperty, propertyName);
Auto.AndCondition andCondition2 = new Auto.AndCondition(typeCondition1, typeCondition2);
target = mainElement.FindFirst(Auto.TreeScope.Descendants, andCondition2);
return target;
}
我终于能够找到带有下面代码的元素,但是真的想要理解为什么上面的代码不起作用。
public Auto.AutomationElement GetElementByIsValuePatternAvailablePropertyAndName(Auto.AutomationElement mainElement,string name,Auto.ControlType controlType)
{
Auto.AutomationElement target = null;
Auto.Condition conditions = new Auto.AndCondition(new Auto.PropertyCondition(Auto.AutomationElement.IsEnabledProperty, true),
new Auto.PropertyCondition(Auto.AutomationElement.IsValuePatternAvailableProperty, true));
// Find all children that match the specified conditions.
Auto.AutomationElementCollection elementCollection = mainElement.FindAll (Auto.TreeScope.Descendants, conditions);
foreach (Auto.AutomationElement ae in elementCollection)
{
if (ae.Current.Name == name)
{
target = ae;
break;
}
}
return target;
}