TreeScope上的自动化FindFirst找不到元素

时间:2012-12-20 18:39:55

标签: automationelement

在搜索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;

}

1 个答案:

答案 0 :(得分:1)

请在项目中添加UIAutomationClient和UIAutomationTypes的程序集引用;-) via martin's answer