/// <summary> /// /// </summary> /// <param name="RelativeType"></param> /// <param name="Condition"></param> /// <returns></returns> public Element GetNextRelative(ElementRelative RelativeType) { IUIAutomationElement relative = null; IUIAutomationTreeWalker walker = new CUIAutomationClass().RawViewWalker; switch (RelativeType) { case ElementRelative.Parent: for (int i = 0; i < 10; i++) { try { relative = walker.GetParentElement(this.IUIElement); break; } catch (COMException e) { } } break; case ElementRelative.FirstChild: for (int i = 0; i < 10; i++) { try { relative = walker.GetFirstChildElement(this.IUIElement); break; } catch (COMException e) { } } break; case ElementRelative.NextSibling: for (int i = 0; i < 10; i++) { try { relative = walker.GetNextSiblingElement(this.IUIElement); break; } catch (COMException e) { } } break; default: throw new Exception("Given enum was not present in the switch!!!"); } if (relative == null) { return(null); } else { return(new Element(relative)); } }
/// <summary> /// <para>This method will continually get the same subsequent relatives until a match is found or no more elements exist (null element returned).</para> /// <para>Useful for stepping down a large tree until the expected element is found.</para> /// <para>Example: Recursively finds the first child of the next element until a condition is hit.</para> /// </summary> /// <param name="RelativeType"></param> /// <param name="Condition"></param> /// <returns></returns> public Element GetRelativeElementUntilFound(ElementRelative RelativeType, SearchCondition Condition, int NumberOfRelatives = 1000) { Console.WriteLine($"--- Start of GetRelativeElementUntilFound Method ---"); //Console.WriteLine($"--- Iterating through Relative Type : {RelativeType.ToString()} ---"); Console.WriteLine($"--- Looking for Condition - By: [{Condition.By}] - ByValue: [{Condition.ByValue}] - LocalizedControlType: [{Condition.ControlType}] ---"); IUIAutomationElement relative = this.IUIElement; IUIAutomationTreeWalker walker = new CUIAutomationClass().RawViewWalker; switch (RelativeType) { case ElementRelative.Parent: for (int i = 0; i < NumberOfRelatives; i++) { try { relative = walker.GetParentElement(relative); //Console.WriteLine($"-- Current Relative Properties - Name: [{relative.Name}] - AutomationId: [{relative.AutomationId}] - LocalizedControlType: [{relative.LocalizedControlType}]"); } catch (COMException e) { } if (relative == null) { break; } else if (MatchElementCondition(relative, Condition)) { break; } } break; case ElementRelative.FirstChild: for (int i = 0; i < NumberOfRelatives; i++) { try { relative = walker.GetFirstChildElement(relative); //Console.WriteLine($"-- Current Relative Properties - Name: [{relative.CurrentName}] - AutomationId: [{relative.CurrentAutomationId}] - LocalizedControlType: [{relative.CurrentLocalizedControlType}]"); } catch (COMException e) { } if (relative == null) { break; } else if (MatchElementCondition(relative, Condition)) { break; } } break; case ElementRelative.NextSibling: for (int i = 0; i < NumberOfRelatives; i++) { try { relative = walker.GetNextSiblingElement(relative); //Console.WriteLine($"-- Current Relative Properties - Name: [{relative.Name}] - AutomationId: [{relative.AutomationId}] - LocalizedControlType: [{relative.LocalizedControlType}]"); } catch (COMException e) { } if (relative == null) { break; } else if (MatchElementCondition(relative, Condition)) { break; } } break; default: throw new Exception("Given enum was not present in the switch!!!"); } if (relative == null) { //Console.WriteLine($"--- End of GetRelativeElementUntilFound Method - Returning Null ---"); return(null); } else { //Console.WriteLine($"--- End of GetRelativeElementUntilFound Method - Returning Element ---"); return(new Element(relative)); } }