There are situations when you have references to Automation Elements which are not valid any longer because the UI tree of the application they belong has changed due to user actions or other reasons.
For example, you have a reference to an AutomationElement which represents a window on your desktop. A runtime, at some point, the user closes the window which means, in your application, you are stucked with an invalid reference you cannot use any more.
To solve this issue, I considered every valid AutomationElement has a ProcessId, because it cannot be that it doesn't belong to any process, and tested this property. If the element is not valid the system throws an ElementNotAvailable exception.
C# sample code:Here is the code to test if an AutomationElement is still valid or not:
© 2017 dDelta Solutions.