SeleniumHQ/selenium · error · InvalidArgumentError
Pass in a CaptureScreenshotParameters object. Received: ${ca
Error message
Pass in a CaptureScreenshotParameters object. Received: ${captureScreenshotParameters} What it means
BrowsingContext.captureScreenshot() accepts an optional CaptureScreenshotParameters instance (or undefined for defaults). If a non-undefined, non-CaptureScreenshotParameters value is passed, it throws InvalidArgumentError. This guards the subsequent captureScreenshotParameters.asMap().forEach() call, which would throw a TypeError on a plain object/null. The check allows undefined (uses defaults) but rejects everything else.
Source
Thrown at javascript/selenium-webdriver/bidi/browsingContext.js:290
params.params = this._driver.validatePrintPageParams(options, params.params)
const response = await this.bidi.send(params)
return new PrintResult(response.result.data)
}
/**
* Captures a screenshot of the browsing context.
*
* @param {CaptureScreenshotParameters|undefined} [captureScreenshotParameters] - Optional parameters for capturing the screenshot.
* @returns {Promise<string>} - A promise that resolves to the base64-encoded string representation of the captured screenshot.
* @throws {InvalidArgumentError} - If the provided captureScreenshotParameters is not an instance of CaptureScreenshotParameters.
*/
async captureScreenshot(captureScreenshotParameters = undefined) {
if (
captureScreenshotParameters !== undefined &&
!(captureScreenshotParameters instanceof CaptureScreenshotParameters)
) {
throw new InvalidArgumentError(
`Pass in a CaptureScreenshotParameters object. Received: ${captureScreenshotParameters}`,
)
}
const screenshotParams = new Map()
screenshotParams.set('context', this._id)
if (captureScreenshotParameters !== undefined) {
captureScreenshotParameters.asMap().forEach((value, key) => {
screenshotParams.set(key, value)
})
}
let params = {
method: 'browsingContext.captureScreenshot',
params: Object.fromEntries(screenshotParams),
}
const response = await this.bidi.send(params)View on GitHub (pinned to aa36b38e69)
Solutions
- Build params via new CaptureScreenshotParameters().origin(...).imageFormat(...) and pass that instance, or omit the argument entirely for defaults.
- If you have no custom params, call captureScreenshot() with no argument (undefined).
- Never pass null; pass nothing or a real CaptureScreenshotParameters instance.
- If building from config, construct the builder and set fields conditionally.
Example fix
// before
await browsingContext.captureScreenshot({ origin: 'viewport', format: 'png' })
// after
const params = new CaptureScreenshotParameters().origin(Origin.VIEWPORT)
await browsingContext.captureScreenshot(params) Defensive patterns
Strategy: type-guard
Validate before calling
if (
captureScreenshotParameters !== undefined &&
!(captureScreenshotParameters instanceof CaptureScreenshotParameters)
) {
throw new TypeError('Pass a CaptureScreenshotParameters instance or omit the argument')
} Type guard
function isCaptureParams(p) {
return p === undefined || p instanceof CaptureScreenshotParameters
} Prevention
- Build params with new CaptureScreenshotParameters().origin(...).imageFormat(...).
- Omit the argument entirely for default screenshot behavior.
- Never pass null; pass nothing or a real instance.
- Do not construct plain config objects.
When it happens
Trigger: Calling captureScreenshot({ origin: 'viewport' }) with a plain object instead of a CaptureScreenshotParameters instance. Passing a Map, a JSON config object, or null (null is not undefined, so it fails). Passing parameters built for a different BiDi command.
Common situations: Constructing params as a plain object from config instead of using the builder class. Passing null explicitly instead of omitting the argument. Copying example code that skips the CaptureScreenshotParameters constructor.
Related errors
- ${msg}
- Pattern must be an instance of UrlPattern. Received: '${patt
- Pattern must be an instance of UrlPattern. Received:'${patte
- Pattern must be an instance of String. Received:'${pattern}'
- Origin must be one of ${Object.values(Origin)}. Received:'${
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/11bd0fc1a66fe8bf.
Report an issue: GitHub.