I have a problem with TreeScope finding an element in Internet Explorer when searching the Name property and ControlType property.
mainElement is the AutomationElement representing "internet Explorer_Server.
all automation elements are listed under mainElement in UISpy.
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;
}
I was finally able to find the element with the code below, but really want to understand why the code above didn't work.
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;
}