public Guid?FindElement(Session session, string locator, string value, Guid?elementId) { var sw = Stopwatch.StartNew(); while (sw.Elapsed < session.Timeouts.Implicit) { var conditions = new List <IUIAutomationCondition>(); switch (locator.ToLowerInvariant()) { case "name": conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ValueValuePropertyId, value)); conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, value)); break; case "id": conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, value)); break; default: throw new NotSupportedException(); } var container = elementId.HasValue ? GetUIAutomationElement(elementId.Value) : _automation.ElementFromHandle(session.Process.MainWindowHandle); var condition = _automation.CreateOrConditionFromArray(conditions.ToArray()); var element = container.FindFirst(TreeScope.TreeScope_Descendants, condition); if (element != null) { return(_elementRepository.AddByHandle(element.CurrentNativeWindowHandle.ToInt32())); } Thread.Sleep(500); } return(null); }