SeleniumHQ/selenium · error · Error

ClipRectangle must be an instance of ClipRectangle. Received

Error message

ClipRectangle must be an instance of ClipRectangle. Received:'${clipRectangle}'

What it means

Thrown by `CaptureScreenshotParameters.clipRectangle()` when the argument is neither a `BoxClipRectangle` nor an `ElementClipRectangle` (the two concrete subclasses exported from `clipRectangle.js`). The method immediately calls `asMap()` on the argument to serialize it into the BiDi command, so it requires an actual instance carrying type/coordinate data — a plain object literal is rejected. Note the message is misleading: it says 'instance of ClipRectangle', but the abstract base `ClipRectangle` class itself also fails the check; only the two exported subclasses pass.

Source

Thrown at javascript/selenium-webdriver/bidi/captureScreenshotParameters.js:85

    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.
   * @returns {CaptureScreenshotParameters} - The current instance of the CaptureScreenshotParameters for chaining.
   */
  clipRectangle(clipRectangle) {
    if (!(clipRectangle instanceof BoxClipRectangle || clipRectangle instanceof ElementClipRectangle)) {
      throw new Error(`ClipRectangle must be an instance of ClipRectangle. Received:'${clipRectangle}'`)
    }
    this.#map.set('clip', Object.fromEntries(clipRectangle.asMap()))
    return this
  }

  asMap() {
    return this.#map
  }
}

module.exports = { CaptureScreenshotParameters, Origin }

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Construct `new BoxClipRectangle(x, y, width, height)` for a fixed region, or `new ElementClipRectangle(sharedId, handleId?)` to clip to an element
  2. Import both from `require('selenium-webdriver/bidi/clipRectangle')`
  3. Do not pass a base `ClipRectangle`, a plain object, or a string

Example fix

// before
params.clipRectangle({ type: 'box', x: 0, y: 0, width: 800, height: 600 })
// after
const { BoxClipRectangle } = require('selenium-webdriver/bidi/clipRectangle')
params.clipRectangle(new BoxClipRectangle(0, 0, 800, 600))
Defensive patterns

Strategy: type-guard

Validate before calling

const { BoxClipRectangle, ElementClipRectangle } = require('selenium-webdriver/bidi/clipRectangle')
const ok = clip instanceof BoxClipRectangle || clip instanceof ElementClipRectangle
if (ok) params.clipRectangle(clip)

Type guard

function isClipRectangle(v) {
  const { BoxClipRectangle, ElementClipRectangle } = require('selenium-webdriver/bidi/clipRectangle')
  return v instanceof BoxClipRectangle || v instanceof ElementClipRectangle
}

Prevention

When it happens

Trigger: Calling `params.clipRectangle({type:'box', x:0, y:0, width:800, height:600})` with a plain object; passing an element shared-id string instead of `new ElementClipRectangle(sharedId)`; passing a base `new ClipRectangle('box')`.

Common situations: Developers used to passing option-object literals to other Selenium BiDi parameter builders try the same here; building a clip rect from JSON-deserialized data; confusing the abstract `ClipRectangle` base with a usable type.

Related errors


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