dotnet/wpf · error · ElementNotAvailableException

ElementNotAvailableException

Error message

ElementNotAvailableException

What it means

WindowsListView.GetNextSibling throws ElementNotAvailableException when the requested item index is greater than or equal to the current list-view item count. The element that previously existed in the tree is gone — the provider refuses to hand out a node for a non-existent item. This is the standard UIA signal for a stale element.

Solutions

  1. Re-find the element from the automation root instead of using the stale sibling reference.
  2. In event handlers, catch ElementNotAvailableException and ignore events for elements that no longer exist.
  3. Pause list updates (or batch them) before automation traverses the tree.
  4. Re-query the item count and adjust traversal logic to the new count.

Example fix

// before
var next = AutomationElement.RootElement.FindFirst(TreeScope.Children, cond); // walks stale chain
// after
try { /* traversal step */ }
catch (ElementNotAvailableException) { return; /* element was removed */ }
Defensive patterns

Strategy: try-catch

Validate before calling

if (itemIndex >= listViewElement.FindAll(TreeScope.Children, Condition.TrueCondition).Count) { /* stale ref; re-find */ }

Try / catch

try { ProcessSibling(element); } catch (ElementNotAvailableException) { /* element removed; abandon traversal */ }

Prevention

When it happens

Trigger: RaiseEventsOnClient walks the sibling chain and asks for the item after 'item', but items were deleted from the Win32 list-view (e.g. LVM_DELETEITEM) so item >= GetItemCount(_hwnd).

Common situations: List contents refreshed/cleared by the app while a UIA event handler is still traversing old element references; bulk list updates firing events mid-mutation.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListView.cs:208

        #endregion ProxySimple Interface

        #region ProxyFragment Interface

        internal override ProxySimple GetNextSibling (ProxySimple child)
        {
            bool hasGroup = IsGroupViewEnabled (_hwnd);
            int item = child._item;
            int count = -1;

            if (!hasGroup)
            {
                // Determine how many items are in the list view.
                count = GetItemCount (_hwnd);

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

                // Check if index of the next item is in range
                if (item >= 0 && (item + 1) < count)
                {
                    // return a node to represent the requested item.
                    return CreateListViewItem (item + 1);
                }
            }

            if (hasGroup)
            {
                if (child is ListViewItem)
                {
                    throw new InvalidOperationException(SR.OperationCannotBePerformed);
                }

                WindowsListViewGroup windowsListViewGroup = child as WindowsListViewGroup;

View on GitHub (pinned to 81131a70a4)