{"record":{"id":"f93c90a2115d4ee5","repo":"dotnet/wpf","slug":"sr-setfocusfailed-automationelement","errorCode":null,"errorMessage":"SR.SetFocusFailed","messagePattern":"SR\\.SetFocusFailed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/AutomationElement.cs","lineNumber":856,"sourceCode":"            return (AutomationPattern[])interfaces.ToArray(typeof(AutomationPattern));\n        }\n\n        /// <summary>\n        /// Request to set focus to this element\n        /// </summary>\n        public void SetFocus()\n        {\n            CheckElement();\n\n            object canReceiveFocus = GetCurrentPropertyValue(AutomationElement.IsKeyboardFocusableProperty);\n\n            if (canReceiveFocus is bool && (bool)canReceiveFocus)\n            {\n                UiaCoreApi.UiaSetFocus(_hnode);\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.SetFocusFailed);\n            }\n        }\n\n        /// <summary>\n        /// Get a point that can be clicked on.  If there is no ClickablePoint return false\n        /// </summary>\n        /// <param name=\"pt\">A point that can be used ba a client to click on this LogicalElement</param>\n        /// <returns>true if there is point that is clickable</returns>\n        public bool TryGetClickablePoint( out Point pt )\n        {\n            // initialize point here so if we return false its initialized\n            pt = new Point (0, 0);\n\n            // Request the provider for a clickable point. \n            object ptClickable = GetCurrentPropertyValue(AutomationElement.ClickablePointProperty);\n\n            if (ptClickable == NotSupported)\n            {","sourceCodeStart":838,"sourceCodeEnd":874,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/AutomationElement.cs#L838-L874","documentation":"SetFocus first asks the provider whether the element can receive focus; only when that check returns true does it call UiaCoreApi.UiaSetFocus. When the element reports it cannot receive focus, the library throws InvalidOperationException rather than attempting a doomed OS focus call.","triggerScenarios":"Calling AutomationElement.SetFocus() on an element whose provider's CanReceiveFocus (via UiaCoreApi.UiaGetPropertyValue / focus-check) returns false — e.g. focus on a disabled control, a label/static text, or an element whose hwnd cannot take keyboard focus.","commonSituations":"Test automation scripts that blindly focus controls; trying to focus containers or read-only elements; elements whose underlying window lost its enabled state at runtime.","solutions":["Check IsEnabled and whether the control is focusable before calling SetFocus","Use TrySetFocus-style logic: catch InvalidOperationException and fall back to clicking the element","Target the inner focusable child (e.g. the edit box inside a combo) instead of the container"],"exampleFix":"// before\nelement.SetFocus();\n// after\nif ((bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty))\n    element.SetFocus();\nelse\n    throw new SkipElementException(\"Element cannot receive focus\");","handlingStrategy":"try-catch","validationCode":"static bool CanFocus(AutomationElement e) =>\n    (bool)e.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) &&\n    !(bool)e.GetCurrentPropertyValue(AutomationElement.IsOffscreenProperty);","typeGuard":"bool IsFocusable(AutomationElement e) =>\n    e.Current.IsEnabled && e.Current.IsKeyboardFocusable;","tryCatchPattern":"try { element.SetFocus(); }\ncatch (InvalidOperationException) { /* element cannot receive focus; use click or skip */ }","preventionTips":["Check IsEnabled/IsKeyboardFocusable before focusing","Prefer interacting with the inner focusable child control","Re-query element state right before focusing in long-running scripts"],"tags":["uiautomation","focus","wpf","invalidoperationexception"],"backgroundTag":"invalid-state-transition","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"}