1

在搜索 Name 属性和 ControlType 属性时,TreeScope 在 Internet Explorer 中查找元素时遇到问题。

mainElement 是表示“Internet Explorer_Server.

UISpy 中的 mainElement 下列出了所有自动化元素。

公共 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;
    }

我终于能够使用下面的代码找到元素,但真的很想了解为什么上面的代码不起作用。

公共 Auto.AutomationElement GetElementByIsValuePatternAvailablePropertyAndName(Auto.AutomationElement mainElement,字符串名称,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;

}

4

1 回答 1

1

请在您的项目中添加 UIAutomationClient 和 UIAutomationTypes 的程序集引用;-) 通过马丁的回答

于 2013-12-07T13:42:00.180 回答