cefsharp/CefSharp · error · ArgumentException

An IFrame instance is required.

Error message

An IFrame instance is required.

What it means

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.

Solutions

  1. Ensure the browser is initialized before accessing MainFrame.
  2. Null-check the frame before calling the extension.
  3. Re-fetch the frame at call time rather than caching.

Example fix

// before
var accepts = await frame.ActiveElementAcceptsTextInput();

// after
if (frame == null || !frame.IsValid) return false;
var accepts = await frame.ActiveElementAcceptsTextInput();
Defensive patterns

Strategy: validation

Validate before calling

if (frame == null || !frame.IsValid) return false;
return await frame.ActiveElementAcceptsTextInput();

Type guard

public static bool IsFrameUsable(IFrame f) => f != null && f.IsValid;

Try / catch

try { return await frame.ActiveElementAcceptsTextInput(); }
catch (ArgumentException ex) when (ex.ParamName == "frame") { return false; }

Prevention

When it happens

Trigger: Calling frame.ActiveElementAcceptsTextInput() when frame is null — typically because MainFrame was accessed before browser initialization or after disposal.

Common situations: Calling the extension on a frame reference captured too early. Race with browser disposal in a WinForms/WPF closing handler.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/70fbfc4ac458859d. Report an issue: GitHub.

Appendix: source

Thrown at CefSharp.Example/ScriptedMethods.cs:26

namespace CefSharp.Example
{
    /// <summary>
    /// Methods whose functionaity is mostly implemented by evaluating or
    /// executing scripts in the browser.
    /// </summary>
    public static class ScriptedMethods
    {
        /// <summary>
        /// Determine if the active element in a frame accepts text input.
        /// </summary>
        /// <param name="frame">Test the active element in this frame.</param>
        /// <returns>True if the active element accepts text input.</returns>
        public static async Task<bool> ActiveElementAcceptsTextInput(this IFrame frame)
        {
            if (frame == null)
            {
                throw new ArgumentException("An IFrame instance is required.", "frame");
            }

            // Scripts should be minified for production builds. The script
            // could also be read from a file...
            const string script =
                  @"(function ()
                    {
                        var isText = false;
                        var activeElement = document.activeElement;
                        if (activeElement) {
                            if (activeElement.tagName.toLowerCase() === 'textarea') {
                                isText = true;
                            } else {
                                if (activeElement.tagName.toLowerCase() === 'input') {
                                    if (activeElement.hasAttribute('type')) {
                                        var inputType = activeElement.getAttribute('type').toLowerCase();
                                        if (inputType === 'text' || inputType === 'email' || inputType === 'password' || inputType === 'tel' || inputType === 'number' || inputType === 'range' || inputType === 'search' || inputType === 'url') {
                                            isText = true;

View on GitHub (pinned to 16bc6e0711)