dotnet/wpf · warning · ElementNotAvailableException

ElementNotAvailableException

Error message

ElementNotAvailableException

What it means

WindowsToolbar.GetNextSibling throws ElementNotAvailableException when the child item's index is >= the toolbar's current Count, meaning the referenced toolbar item no longer exists. UIA uses this exception to signal that an element (or its sibling position) has vanished from the tree, typically because the UI changed.

Solutions

  1. Re-find the toolbar element after UI changes instead of using cached references
  2. Catch ElementNotAvailableException and restart the tree walk from the toolbar
  3. Subscribe to UIA structure-changed events instead of blind sibling navigation

Example fix

// before
var next = walker.GetNextSibling(cachedButton);
// after
try { next = walker.GetNextSibling(cachedButton); } catch (ElementNotAvailableException) { toolbar = ReFind(); next = walker.GetFirstChild(toolbar); }
Defensive patterns

Strategy: try-catch

Validate before calling

int count = CountToolbarButtons(toolbar); if (cachedIndex >= count) { cachedButton = RefindButton(toolbar); }

Try / catch

try { next = walker.GetNextSibling(btn); } catch (ElementNotAvailableException) { next = RefindAndGetSibling(toolbar, btnName); }

Prevention

When it happens

Trigger: UIA TreeWalker.GetNextSibling() called on a toolbar button whose _item index is stale because buttons were added/removed (Count changed) between fetching the element and walking the tree.

Common situations: Dynamic toolbars with buttons shown/hidden or removed at runtime; automation caches elements across UI updates; slow clients reading a rebuilt toolbar.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/b213e568340c1cec. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsToolbar.cs:126

        //
        //  Patterns Implementation
        //
        //------------------------------------------------------

        #region ProxyFragment Interface

        // Returns the next sibling element in the raw hierarchy.
        // Peripheral controls have always negative values.
        // Returns null if no next child.
        internal override ProxySimple GetNextSibling (ProxySimple child)
        {
            ProxySimple toolbarItem = null;
            int count = Count;

            // Next for an item that does not exist in the list
            if (child._item >= count)
            {
                throw new ElementNotAvailableException ();
            }

            // If the index of the next node would be out of range...
            for (int item = child._item + 1; item >= 0 && item < count; item++)
            {
                // This may fail if the toolbar item is hidden
                if ((toolbarItem = CreateToolbarItem (item)) != null)
                {
                    break;
                }
            }

            return toolbarItem;
        }

        // Returns the previous sibling element in the raw hierarchy.
        // Peripheral controls have always negative values.
        // Returns null is no previous.

View on GitHub (pinned to 81131a70a4)