SeleniumHQ/selenium · error · InvalidArgumentError

${msg}

Error message

${msg}

What it means

BrowsingContext.checkErrorInScreenshot inspects the BiDi response object; if it contains an 'error' key equal to 'invalid argument', it throws InvalidArgumentError with the server-supplied msg. This surfaces server-side validation failures from the browsingContext.captureScreenshot command — e.g. an unsupported image format, an out-of-range clip rectangle, or an invalid origin. The dynamic msg comes from the remote end (browser/driver), so its content varies.

Source

Thrown at javascript/selenium-webdriver/bidi/browsingContext.js:365

            sharedId: sharedId,
            handle: handle,
          },
        },
      },
    }

    const response = await this.bidi.send(params)
    this.checkErrorInScreenshot(response)
    return response['result']['data']
  }

  checkErrorInScreenshot(response) {
    if ('error' in response) {
      const { error, msg } = response

      switch (error) {
        case 'invalid argument':
          throw new InvalidArgumentError(msg)

        case 'no such frame':
          throw new NoSuchFrameError(msg)
      }
    }
  }

  /**
   * Activates and focuses the top-level browsing context.
   * @returns {Promise<void>} A promise that resolves when the browsing context is activated.
   * @throws {Error} If there is an error while activating the browsing context.
   */
  async activate() {
    const params = {
      method: 'browsingContext.activate',
      params: {
        context: this._id,
      },

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Validate image format against supported values (typically 'png'/'jpeg') before sending; prefer 'png'.
  2. Ensure clip rectangle width/height are positive finite numbers.
  3. Keep quality within [0,100].
  4. Upgrade the browser/driver to a version supporting the requested BiDi screenshot options.
  5. Inspect the server msg in the caught InvalidArgumentError for the specific rejected field.

Example fix

// before
params.imageFormat('webp', 150)
await browsingContext.captureScreenshot(params)

// after
params.imageFormat('png')
await browsingContext.captureScreenshot(params)
Defensive patterns

Strategy: try-catch

Validate before calling

const SUPPORTED = ['png', 'jpeg']
if (type && !SUPPORTED.includes(type)) {
  throw new Error('Unsupported screenshot image format: ' + type)
}

Try / catch

try {
  data = await browsingContext.captureScreenshot(params)
} catch (e) {
  if (e instanceof InvalidArgumentError) {
    // inspect e.message for the rejected field; adjust params and retry
  } else throw e
}

Prevention

When it happens

Trigger: Requesting an image format the remote end does not support (e.g. 'webp' on an older build). Supplying a clip rectangle with negative width/height. An origin value the driver rejects. A quality value outside [0,100] that the server validates. Requesting a screenshot of a context that is not in a capturable state.

Common situations: Driver/browser version mismatch where a BiDi screenshot feature is unsupported. Passing quality > 100 or < 0. Clip rectangles computed from stale element geometry yielding invalid dimensions. Headless vs. headed differences in supported formats.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/21708b836b2166c1. Report an issue: GitHub.