dotnet/wpf · warning · System.Windows.Automation.ElementNotEnabledException
ElementNotEnabledException
Error message
ElementNotEnabledException
What it means
WindowsMenu's IInvokeProvider.Invoke throws ElementNotEnabledException when the Win32 menu item this UI Automation proxy wraps is currently disabled (IsEnabledMenu is false). UIA providers must refuse Invoke on disabled elements, and this exception tells the automation client that the element exists but cannot be activated yet. It is the standard UIA way to signal 'control is not enabled', not a crash in the provider.
Solutions
- Check the element's IsEnabled property (via IElementProvider/automation pattern) before calling Invoke and skip or wait until it is enabled
- Enable the menu item in the target application (correct the app state that grayed it out) before invoking
- Catch ElementNotEnabledException in the automation client and treat it as 'element disabled' rather than a test failure
- Use UIA WaitForElement/condition polling so the invoke only happens once IsEnabled becomes true
Example fix
// before
menuItem.Invoke(); // throws if disabled
// after
if (menuItem.Current.IsEnabled)
{
menuItem.Invoke();
}
else
{
// wait or skip; element is grayed out
} Defensive patterns
Strategy: validation
Validate before calling
if (!menuItem.Current.IsEnabled)
throw new InvalidOperationException("Menu item is disabled; cannot Invoke."); Type guard
bool CanInvoke(AutomationElement e) => e != null && e.Current.IsEnabled;
Try / catch
try { menuItem.Invoke(); }
catch (ElementNotEnabledException) { /* element grayed out; skip or wait */ } Prevention
- Always read IsEnabled before Invoke on UIA elements
- Use UIA conditions/polling to wait until elements are enabled
- Model grayed-out menu items as skipped steps in test frameworks
- Check the target app's command state (e.g. selection exists) before menu automation
When it happens
Trigger: Calling Invoke() via UI Automation on a menu item whose owning Win32 menu is grayed out/disabled (IsEnabledMenu false), e.g. invoking an unavailable menu item from a test or assistive tool.
Common situations: UI test automation clicks/invokes a menu item that the application disabled because its command is unavailable in the current app state (e.g. Copy with nothing selected); apps with license or state-dependent grayed menu entries.
Related errors
- ElementNotEnabledException
- ElementNotEnabledException
- ElementNotEnabledException
- ElementNotEnabledException
- ElementNotEnabledException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ab73d96e026cd113.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsMenu.cs:1667
return this;
}
return null;
}
#endregion ProxyFragment Interface
#region Invoke Pattern
// Select the desired menuItem. This will expand a menu item as well as
// invoke a menu item. The enter key is used here to make sure in the
// case where we are not expanding that the menu get dissmissed properly.
void IInvokeProvider.Invoke ()
{
// Make sure that the control is enabled
if (!IsEnabledMenu)
{
throw new ElementNotEnabledException();
}
SetFocus();
// sending enter key to the currently selected item
ExpandViaEnter();
WaitForMenuMode(false);
}
#endregion
#region ExpandCollapse Pattern
// Show all Children
void IExpandCollapseProvider.Expand ()
{
if (!IsEnabledMenu)
{View on GitHub (pinned to 81131a70a4)