dotnet/wpf · error · ElementNotEnabledException

Element is not enabled.

Error message

Element is not enabled.

What it means

WindowsHyperlink's Invoke pattern checks IsWindowEnabled on the underlying hyperlink control before performing the click; a disabled control yields ElementNotEnabledException per the UIA Invoke pattern requirements.

Solutions

  1. Enable the control before invoking (fix app state that disables it).
  2. Poll/wait for element.Current.IsEnabled == true (with timeout) before Invoke.
  3. Catch ElementNotEnabledException and retry after the enabling condition is met.
  4. If automating end-to-end, complete the prerequisite step (e.g. accept terms) that enables the link first.

Example fix

// before
((InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
// after
var sw = Stopwatch.StartNew();
while (!element.Current.IsEnabled && sw.ElapsedMilliseconds < 5000) Thread.Sleep(100);
if (element.Current.IsEnabled) ((InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
Defensive patterns

Strategy: retry

Validate before calling

bool enabled = element.Current.IsEnabled;
if (!enabled) { /* wait or perform prerequisite action */ }

Type guard

static bool ReadyToInvoke(AutomationElement e) => e.Current.IsEnabled && !e.Current.IsOffscreen && e.TryGetCurrentPattern(InvokePattern.Pattern, out _);

Try / catch

try { invokePattern.Invoke(); }
catch (ElementNotEnabledException) { WaitForEnabled(element, TimeSpan.FromSeconds(5)); invokePattern.Invoke(); }

Prevention

When it happens

Trigger: Invoking the hyperlink automation element while the control is disabled — Enabled=false set by app logic, disabled parent container, or disabled state pending a condition (e.g. terms not accepted).

Common situations: Automating dialogs where the hyperlink unlocks after validation; disabled links in installers/wizards; scripts racing ahead of the app enabling the control.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsHyperlink.cs:424

            }
        }

        #endregion ProxySimple Interface

        #region Invoke Pattern

        // Same as clicking on an hyperlink
        void IInvokeProvider.Invoke()
        {
            // Check that button can be clicked.
            //
            // This state could change anytime.
            //

            // Make sure that the control is enabled
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            if (!SafeNativeMethods.IsWindowVisible(_hwnd))
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }

            //
            // Get the bounding rect for the window.
            //
            NativeMethods.Win32Rect BoundingRect = NativeMethods.Win32Rect.Empty;
            if (!Misc.GetWindowRect(_hwnd, ref BoundingRect))
            {
                return;
            }

            //
            // All we really need here are the height and the width,

View on GitHub (pinned to 81131a70a4)