{"record":{"id":"aa9d02d383402181","repo":"dotnet/wpf","slug":"new-elementnotavailableexception","errorCode":null,"errorMessage":"new ElementNotAvailableException()","messagePattern":"new ElementNotAvailableException\\(\\)","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Accessible.cs","lineNumber":917,"sourceCode":"        // Returns an array of IAccessible interfaces and/or childID's representing the children\n        internal static object[] GetAccessibleChildren(IAccessible accessibleObject, out int childrenReturned)\n        {\n            int childCount;\n            object[] aChildren = null;\n\n            try\n            {\n                childCount = accessibleObject.accChildCount;\n                childrenReturned = 0;\n                if (childCount > 0)\n                {\n                    aChildren = new object[childCount];\n\n                    // Get the raw children because accNavigate doesn't work\n                    if (UnsafeNativeMethods.AccessibleChildren(accessibleObject, 0, childCount, aChildren, out childrenReturned) == NativeMethods.E_INVALIDARG)\n                    {\n                        System.Diagnostics.Debug.Fail(\"Call to AccessibleChildren() returned E_INVALIDARG.\");\n                        throw new ElementNotAvailableException();\n                    }\n                }\n\n                return aChildren;\n            }\n            catch (Exception e)\n            {\n                if (HandleIAccessibleException(e))\n                {\n                    throw;\n                }\n                throw new ElementNotAvailableException();\n            }\n        }\n\n        #endregion\n\n        #endregion Internal Methods","sourceCodeStart":899,"sourceCodeEnd":935,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Accessible.cs#L899-L935","documentation":"GetAccessibleChildren throws ElementNotAvailableException when the Win32 AccessibleChildren call returns E_INVALIDARG. The wrapper treats this as evidence the accessible object is no longer available/valid (child enumeration impossible), which in UIA terms means the element is dead.","triggerScenarios":"Enumerating children of an MSAA accessible whose childCount changed or whose underlying object was destroyed between querying Accessible.ChildCount and calling AccessibleChildren; passing an accessible object from a closed window/control.","commonSituations":"Walking UI trees of apps that rebuild controls dynamically (toolbars, virtualized lists, browser content); timing races where the container is reparented or destroyed during traversal; legacy MSAA apps with buggy child-count reporting.","solutions":["Catch ElementNotAvailableException during tree traversal and treat the element as removed — rescan from the root.","Re-query the element/children promptly after obtaining childCount to shrink the race window; add retries with delay.","Use UIA's raw view walker / TreeWalker instead of direct MSAA enumeration where possible, as it handles dynamic trees better.","If the target app is yours, ensure its IAccessible get_accChildCount/get_accChild agree and keep objects alive during enumeration."],"exampleFix":"// before\nvar children = GetAccessibleChildren(accessible); // element died mid-call\n// after\ntry { var children = GetAccessibleChildren(accessible); }\ncatch (ElementNotAvailableException)\n{\n    accessible = null; // element no longer available; rescan tree\n}","handlingStrategy":"try-catch","validationCode":"int count = accessible.ChildCount; // re-read and re-validate immediately before enumerating children; abort if the element reports 0 or the window is gone","typeGuard":null,"tryCatchPattern":"try\n{\n    var children = GetAccessibleChildren(accessible);\n}\ncatch (ElementNotAvailableException)\n{\n    // element vanished mid-enumeration; rescan the tree from the root\n    accessible = null;\n    RetryTreeWalk();\n}","preventionTips":["Wrap every child-enumeration step in ElementNotAvailableException handling","Rescan from the root instead of reusing stale accessible references","Minimize delay between ChildCount and AccessibleChildren calls","On heavily dynamic UIs, prefer UIA TreeWalker over MSAA child enumeration","Retry the walk a bounded number of times with small delays before giving up"],"tags":["uiautomation","msaa","element-not-available","stale-element","tree-walking"],"backgroundTag":"element-not-available","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}