{"record":{"id":"70fbfc4ac458859d","repo":"cefsharp/CefSharp","slug":"an-iframe-instance-is-required","errorCode":null,"errorMessage":"An IFrame instance is required.","messagePattern":"An IFrame instance is required\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"CefSharp.Example/ScriptedMethods.cs","lineNumber":26,"sourceCode":"\nnamespace CefSharp.Example\n{\n    /// <summary>\n    /// Methods whose functionaity is mostly implemented by evaluating or\n    /// executing scripts in the browser.\n    /// </summary>\n    public static class ScriptedMethods\n    {\n        /// <summary>\n        /// Determine if the active element in a frame accepts text input.\n        /// </summary>\n        /// <param name=\"frame\">Test the active element in this frame.</param>\n        /// <returns>True if the active element accepts text input.</returns>\n        public static async Task<bool> ActiveElementAcceptsTextInput(this IFrame frame)\n        {\n            if (frame == null)\n            {\n                throw new ArgumentException(\"An IFrame instance is required.\", \"frame\");\n            }\n\n            // Scripts should be minified for production builds. The script\n            // could also be read from a file...\n            const string script =\n                  @\"(function ()\n                    {\n                        var isText = false;\n                        var activeElement = document.activeElement;\n                        if (activeElement) {\n                            if (activeElement.tagName.toLowerCase() === 'textarea') {\n                                isText = true;\n                            } else {\n                                if (activeElement.tagName.toLowerCase() === 'input') {\n                                    if (activeElement.hasAttribute('type')) {\n                                        var inputType = activeElement.getAttribute('type').toLowerCase();\n                                        if (inputType === 'text' || inputType === 'email' || inputType === 'password' || inputType === 'tel' || inputType === 'number' || inputType === 'range' || inputType === 'search' || inputType === 'url') {\n                                            isText = true;","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Example/ScriptedMethods.cs#L8-L44","documentation":"Thrown by ScriptedMethods.ActiveElementAcceptsTextInput when the frame argument is null. The extension method needs a live IFrame to execute JavaScript that inspects document.activeElement. Note it uses ArgumentException (not ArgumentNullException) and the older string param name form.","triggerScenarios":"Calling frame.ActiveElementAcceptsTextInput() when frame is null — typically because MainFrame was accessed before browser initialization or after disposal.","commonSituations":"Calling the extension on a frame reference captured too early. Race with browser disposal in a WinForms/WPF closing handler.","solutions":["Ensure the browser is initialized before accessing MainFrame.","Null-check the frame before calling the extension.","Re-fetch the frame at call time rather than caching."],"exampleFix":"// before\nvar accepts = await frame.ActiveElementAcceptsTextInput();\n\n// after\nif (frame == null || !frame.IsValid) return false;\nvar accepts = await frame.ActiveElementAcceptsTextInput();","handlingStrategy":"validation","validationCode":"if (frame == null || !frame.IsValid) return false;\nreturn await frame.ActiveElementAcceptsTextInput();","typeGuard":"public static bool IsFrameUsable(IFrame f) => f != null && f.IsValid;","tryCatchPattern":"try { return await frame.ActiveElementAcceptsTextInput(); }\ncatch (ArgumentException ex) when (ex.ParamName == \"frame\") { return false; }","preventionTips":["Ensure browser is initialized before accessing MainFrame.","Null-check frame before calling scripted extensions.","Check IsValid as well as null."],"tags":["argument-null","frame","scripted-methods","example-code"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}