Я создал несколько методов расширения, чтобы несколько упростить работу с AutomationElement. Я вставил соответствующие ниже, но вы можете прочитать больше о них здесь .
Я предполагаю, что у вас есть ссылка на корневое окно IE. Если нет, но вы знаете, что это Process Id, вы можете найти его так:
var ieWindow = AutomationElement.RootElement.FindChildByCondition(new PropertyCondition(AutomationElement.ProcessIdProperty, ieProcessId));
Предполагая, что в IE открыт только один фрейм, и один элемент управления Silverlight, вы можете сделать:
var silverlightControl = ieWindow.FindDescendentByClassPath(
new[]{
"Frame Tab",
"TabWindowClass",
"Shell DocObject View",
"Internet Explorer_Server",
"MicrosoftSilverlight",
});
Если у вас есть несколько элементов управления Silverlight, я не знаю, как их отличить с помощью UIAutomation. Я бы попытался удалить запись «MicrosoftSilverlight» из пути к классу выше, чтобы вы получили ссылку на страницу проводника. Тогда используйте
AutomationElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MicrosoftSilverlight"))
, чтобы найти все SilverlightControls, затем проверять их, каждый из которых по очереди находит какой-то элемент внутри них, который позволяет вам различать их.
Вот методы расширения:
public static class AutomationExtensions
{
public static AutomationElement FindDescendentByClassPath(this AutomationElement element, IEnumerable<string> classNames)
{
var conditionPath = CreateClassNameConditionPath(classNames);
return element.FindDescendentByConditionPath(conditionPath);
}
public static AutomationElement FindDescendentByConditionPath(this AutomationElement element, IEnumerable<Condition> conditionPath)
{
if (!conditionPath.Any())
{
return element;
}
var result = conditionPath.Aggregate(
element,
(parentElement, nextCondition) => parentElement == null
? null
: parentElement.FindChildByCondition(nextCondition));
return result;
}
public static AutomationElement FindChildByCondition(this AutomationElement element, Condition condition)
{
var result = element.FindFirst(
TreeScope.Children,
condition);
return result;
}
public static IEnumerable<Condition> CreateClassNameConditionPath(IEnumerable<string> classNames)
{
return classNames.Select(name => new PropertyCondition(AutomationElement.ClassNameProperty, name, PropertyConditionFlags.IgnoreCase)).ToArray();
}
}