In my test application, I am constantly opening and re-opening a form. Everytime the form is opened, I must get all the elements on the form into an AutomationElementCollection
so that I can operate on the elements. However, it seems expensive to repeatedly get these elements (due to tree navigation/context-switches etc.).
I attempted to set a boolean around the method that gets the elements. If the method was called for the first time, it would run normally, and set the boolean to true. If the method gets called a second time it will do nothing, as the array has already been populated.
However when I try to perform operations on any AutomationElement
in the array (for a second time), the elements do not seem to be available. Does closing the form somehow "disable" these elements? Do I HAVE to find these elements each time I open the form, so that they are "fresh"?
I looked at the CacheRequest
way, but that seems to only pertain to accessing properties/patterns, not elements.
Here is code/error message:
AutomationElement GAP;
AutomationElementcollection GAP1;
private bool initGAP1 = false;
public void initGAP()
{
if (!initGAP1)
{
int refnum = ...;
int refnum2 = ...;
AutomationElementCollection temp = MMChildren[refnum].FindAll(TreeScope.Children, findCondition);
GAP = temp.FindAll(TreeScope.Children, findCondition)[refnum2];
GAP1 = GAP.FindAll(TreeScope.Children, findCondition); //this contains the elements I want to operate on
initGAP1 = true;
}
}
System.Windows.Automation.ElementNotEnabledException: Exception of type 'System.Windows.Automation.ElementNotEnabledException' was thrown.