{"record":{"id":"4afc62b9930a6137","repo":"dotnet/wpf","slug":"uia-e-elementnotavailable","errorCode":"UIA_E_ELEMENTNOTAVAILABLE","errorMessage":"description (from UiaGetErrorDescription or SR.UnknownCoreAPIError)","messagePattern":"description \\(from UiaGetErrorDescription or SR\\.UnknownCoreAPIError\\)","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/UiaCoreApi.cs","lineNumber":1191,"sourceCode":"\n            // Handle these UIA exceptions specially - COM Interop\n            // maps others (eg. InvalidArgument), but not these:\n            if (hr == UIA_E_ELEMENTNOTENABLED\n            || hr == UIA_E_ELEMENTNOTAVAILABLE\n            || hr == UIA_E_NOCLICKABLEPOINT\n            || hr == UIA_E_PROXYASSEMBLYNOTLOADED)\n            {\n                string description;\n                if (!UiaGetErrorDescription(out description))\n                    description = SR.UnknownCoreAPIError;\n\n                switch (hr)\n                {\n                    case UIA_E_ELEMENTNOTENABLED:\n                        throw new ElementNotEnabledException(description);\n\n                    case UIA_E_ELEMENTNOTAVAILABLE:\n                        throw new ElementNotAvailableException(description);\n\n                    case UIA_E_NOCLICKABLEPOINT:\n                        throw new NoClickablePointException(description);\n\n                    case UIA_E_PROXYASSEMBLYNOTLOADED:\n                        throw new ProxyAssemblyNotLoadedException(description);\n                }\n            }\n\n            // Not a UIA-specific exception - let COM Interop handle the rest.\n            // (Marshal.ThrowExceptionForHR automatically calls GetErrorInfo to fill in the description\n            // field - UIA sets the error info itself, so it will get propogated here.)\n            Marshal.ThrowExceptionForHR(hr);\n        }\n\n        #endregion Private Methods\n\n        #region Raw API methods","sourceCodeStart":1173,"sourceCodeEnd":1209,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/UiaCoreApi.cs#L1173-L1209","documentation":"UiaCoreApi maps the native HRESULT UIA_E_ELEMENTNOTAVAILABLE to ElementNotAvailableException. This is thrown when an automation element that the client held a reference to is no longer available - the underlying UI element was destroyed, closed, or its runtime ID is stale. UiaGetErrorDescription supplies the message.","triggerScenarios":"Any native UIA call made through a stale AutomationElement reference that returns UIA_E_ELEMENTNOTAVAILABLE (0x80040201), e.g. accessing Current properties or invoking a pattern on an element after its window closed or the item was removed from a list.","commonSituations":"Holding AutomationElement references across UI rebuilds (lists/virtualized grids recycle items); reacting to an event from an element whose window then closes; menu items that disappear between hooking and invoking.","solutions":["Wrap access to element properties/patterns in try-catch for ElementNotAvailableException and treat it as 'element gone'","Re-resolve the element freshly from the desktop/app window instead of reusing cached references","Subscribe to the WindowClosed / structure-changed events and drop cached elements when they fire","Reduce reliance on stale references in event handlers: re-find the element by AutomationId at handling time"],"exampleFix":"// before\nvar name = cachedItem.Current.Name;\n// after\ntry { var name = cachedItem.Current.Name; }\ncatch (ElementNotAvailableException) { cachedItem = FindFreshItem(); }","handlingStrategy":"try-catch","validationCode":"// No reliable pre-check; validate element is alive just before use\nif (element.GetRuntimeId() == null) ReacquireElement();","typeGuard":"static bool IsAlive(AutomationElement e) { try { var _ = e.Current.Name; return true; } catch (ElementNotAvailableException) { return false; } }","tryCatchPattern":"try { var name = element.Current.Name; }\ncatch (ElementNotAvailableException) { element = ReacquireElement(); }","preventionTips":["Never cache AutomationElement references across UI structure changes","Re-find elements by AutomationId at point of use","Drop references on WindowClosed/structure-changed events"],"tags":["uiautomation","wpf","stale-element"],"backgroundTag":"entity-not-found","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}