public static AutomationElement FindUnderElement(AutomationElement ParentWindow, string @ElemName, string @ElemClass, string @ElemAutoID, string ElemType, int Timeout, int idx = 1) { AutomationElement _ReturnElement = null; ElemName = ElemName.Replace("*", ".*").Replace("(", "\\(").Replace(")", "\\)"); ElemClass = ElemClass.Replace("*", ".*").Replace("(", "\\(").Replace(")", "\\)"); ElemAutoID = ElemAutoID.Replace("*", ".*").Replace("(", "\\(").Replace(")", "\\)"); Regex NameRegex = new Regex(ElemName, RegexOptions.IgnoreCase); Regex ClassRegex = new Regex(ElemClass, RegexOptions.IgnoreCase); Regex AutoIDRegex = new Regex(ElemAutoID, RegexOptions.IgnoreCase); var StartTime = DateTime.Now; var EndTime = StartTime.AddMilliseconds(double.Parse(Timeout.ToString())); while (DateTime.Now <= EndTime && _ReturnElement == null) { List <AutomationElement> AllDesc = GetAllChildren(ParentWindow); //ParentWindow.FindAll(TreeScope.Descendants, System.Windows.Automation.Condition.TrueCondition).Cast<AutomationElement>().ToList(); List <AutomationElement> TypeFiltered = AllDesc.Where(x => x.Current.ControlType.ProgrammaticName.ToLower().Split('.')[1] == ElemType.ToLower()).ToList(); List <AutomationElement> NameFiltered = TypeFiltered.Where(x => NameRegex.Match(x.Current.Name).Success).ToList(); List <AutomationElement> ClassFiltered = NameFiltered.Where(x => ClassRegex.Match(x.Current.ClassName).Success).ToList(); List <AutomationElement> AutoIDFiltered = ClassFiltered.Where(x => AutoIDRegex.Match(x.Current.AutomationId).Success).ToList(); List <AutomationElement> Filtered = AutoIDFiltered; _ReturnElement = Filtered.FirstOrDefault(); //MessageBox.Show(TypeFiltered.Count.ToString() + " : " + NameFiltered.Count.ToString() + " : " + ClassFiltered.Count.ToString() + " : " + AutoIDFiltered.Count.ToString() + " : " + Filtered.Count.ToString()); //MessageBox.Show(string.Join(",", TypeFiltered.Select(x => x.Current.Name))); if (Filtered.Count >= idx) { _ReturnElement = Filtered[idx - 1]; return(_ReturnElement); } else { _ReturnElement = null; } } return(_ReturnElement); }
public static AutomationElement FindAtIndex(AutomationElement Elem, string @ElemName, string @ElemClass, string ElemType, string ElemAutoID, int Timeout, int idx = 1) { ElemName = String.IsNullOrEmpty(ElemName.Trim()) ? "*" : ElemName.Replace("*", ".*").Replace("(", "\\(").Replace(")", "\\)"); ElemClass = String.IsNullOrEmpty(ElemClass.Trim()) ? "*" : ElemClass.Replace("*", ".*").Replace("(", "\\(").Replace(")", "\\)"); ElemAutoID = String.IsNullOrEmpty(ElemAutoID.Trim()) ? "*" : ElemAutoID.Replace("*", ".*").Replace("(", "\\(").Replace(")", "\\)"); Regex NameRegex = new Regex(ElemName, RegexOptions.IgnoreCase); Regex ClassRegex = new Regex(ElemClass, RegexOptions.IgnoreCase); Regex AutoIDRegex = new Regex(ElemAutoID, RegexOptions.IgnoreCase); AutomationElement _ReturnElement = null; var StartTime = DateTime.Now; var EndTime = StartTime.AddMilliseconds(double.Parse(Timeout.ToString())); while (DateTime.Now < EndTime && _ReturnElement == null) { TreeWalker walker = TreeWalker.ControlViewWalker; AutomationElement child = walker.GetFirstChild(Elem); int indexCounter = 0; while (child != null) { //FirstChildren.Add(child); try { if (child.Current.ControlType.ProgrammaticName.ToLower().Split('.')[1] == ElemType.ToLower()) { if (NameRegex.Match(child.Current.Name).Success) { if (ClassRegex.Match(child.Current.ClassName).Success) { if (AutoIDRegex.Match(child.Current.AutomationId).Success) { indexCounter += 1; if (indexCounter == idx) { return(child); } } } } } child = walker.GetNextSibling(child); } catch (COMException) { child = null; } } /* * List<AutomationElement> TypeFiltered = FirstChildren.Where(x => x.Current.ControlType.ProgrammaticName.ToLower().Split('.')[1] == ElemType.ToLower()).ToList(); * List<AutomationElement> NameFiltered = TypeFiltered.Where(x => NameRegex.Match(x.Current.Name).Success).ToList(); * List<AutomationElement> ClassFiltered = NameFiltered.Where(x => ClassRegex.Match(x.Current.ClassName).Success).ToList(); * * if (ClassFiltered.Count >= idx) * { * _ReturnElement = ClassFiltered[idx - 1]; * return _ReturnElement; * } * else * { * _ReturnElement = null; * } * } */ } return(null); }