SeleniumHQ/selenium · error · Error
Type must be an instance of String. Received:'${type}'
Error message
Type must be an instance of String. Received:'${type}' What it means
CaptureScreenshotParameters.imageFormat() validates the first argument (type) is a primitive string (typeof === 'string') before setting it as the image format. This is the format/codec selector (e.g. 'png', 'jpeg'). Passing a number, object, null, undefined, or a String wrapper object fails the typeof check. Note this only validates that type is a string, not that it is a supported format — unsupported formats are caught later server-side (error [15]).
Source
Thrown at javascript/selenium-webdriver/bidi/captureScreenshotParameters.js:62
origin(origin) {
if (origin !== Origin.VIEWPORT && origin !== Origin.DOCUMENT) {
throw new Error(`Origin must be one of ${Object.values(Origin)}. Received:'${origin}'`)
}
this.#map.set('origin', origin)
return this
}
/**
* Sets the image format and quality for capturing a screenshot.
*
* @param {string} type - The image format type.
* @param {number} [quality] - The image quality (optional).
* @throws {Error} If the type is not a string or if the quality is not a number.
* @returns {CaptureScreenshotParameters} - The current instance of the CaptureScreenshotParameters for chaining.
*/
imageFormat(type, quality = undefined) {
if (typeof type !== 'string') {
throw new Error(`Type must be an instance of String. Received:'${type}'`)
}
this.#map.set('type', type)
if (quality !== undefined) {
if (typeof quality !== 'number') {
throw new Error(`Quality must be a number. Received:'${quality}'`)
}
this.#map.set('quality', quality)
}
return this
}
/**
* Sets the clip rectangle for capturing a screenshot.
*
* @param {BoxClipRectangle|ElementClipRectangle} clipRectangle - The clip rectangle to set.
* @throws {Error} If the clipRectangle is not an instance of ClipRectangle.View on GitHub (pinned to aa36b38e69)
Solutions
- Pass a primitive string format as the first argument: imageFormat('png') or imageFormat('jpeg', 80).
- If you only want to set quality, you must still supply a type string first.
- Use lowercase canonical format names ('png', 'jpeg').
- Validate config values are strings before forwarding.
Example fix
// before
params.imageFormat(80) // number where type string expected
// after
params.imageFormat('jpeg', 80) Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof type !== 'string') {
throw new TypeError('imageFormat type must be a string (e.g. png, jpeg)')
} Type guard
function isImageFormatType(t) {
return typeof t === 'string'
} Prevention
- Always pass the format string as the first argument.
- Use canonical lowercase names ('png','jpeg').
- Validate config values are strings before forwarding.
- Avoid String object wrappers.
When it happens
Trigger: Calling imageFormat(undefined) or imageFormat(null) for the type. Passing a numeric or object value. Passing a String object wrapper (typeof 'object'). Forgetting the type argument when also wanting to set quality.
Common situations: Calling imageFormat(quality) positionally (passing quality where type belongs). Config code forwarding a non-string. Building the call dynamically and omitting the type.
Related errors
- Quality must be a number. Received:'${quality}'
- Origin must be one of ${Object.values(Origin)}. Received:'${
- ClipRectangle must be an instance of ClipRectangle. Received
- Http method must be a string. Received: '${method})'
- Url must be a string. Received:'${url}'
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/a237bd11f7047b18.
Report an issue: GitHub.