{"record":{"id":"cf3fc2f16846758d","repo":"SeleniumHQ/selenium","slug":"cliprectangle-must-be-an-instance-of-cliprectangle","errorCode":null,"errorMessage":"ClipRectangle must be an instance of ClipRectangle. Received:'${clipRectangle}'","messagePattern":"ClipRectangle must be an instance of ClipRectangle\\. Received:'(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/bidi/captureScreenshotParameters.js","lineNumber":85,"sourceCode":"    if (quality !== undefined) {\n      if (typeof quality !== 'number') {\n        throw new Error(`Quality must be a number. Received:'${quality}'`)\n      }\n      this.#map.set('quality', quality)\n    }\n    return this\n  }\n\n  /**\n   * Sets the clip rectangle for capturing a screenshot.\n   *\n   * @param {BoxClipRectangle|ElementClipRectangle} clipRectangle - The clip rectangle to set.\n   * @throws {Error} If the clipRectangle is not an instance of ClipRectangle.\n   * @returns {CaptureScreenshotParameters} - The current instance of the CaptureScreenshotParameters for chaining.\n   */\n  clipRectangle(clipRectangle) {\n    if (!(clipRectangle instanceof BoxClipRectangle || clipRectangle instanceof ElementClipRectangle)) {\n      throw new Error(`ClipRectangle must be an instance of ClipRectangle. Received:'${clipRectangle}'`)\n    }\n    this.#map.set('clip', Object.fromEntries(clipRectangle.asMap()))\n    return this\n  }\n\n  asMap() {\n    return this.#map\n  }\n}\n\nmodule.exports = { CaptureScreenshotParameters, Origin }\n","sourceCodeStart":67,"sourceCodeEnd":97,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/bidi/captureScreenshotParameters.js#L67-L97","documentation":"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.","triggerScenarios":"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')`.","commonSituations":"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.","solutions":["Construct `new BoxClipRectangle(x, y, width, height)` for a fixed region, or `new ElementClipRectangle(sharedId, handleId?)` to clip to an element","Import both from `require('selenium-webdriver/bidi/clipRectangle')`","Do not pass a base `ClipRectangle`, a plain object, or a string"],"exampleFix":"// before\nparams.clipRectangle({ type: 'box', x: 0, y: 0, width: 800, height: 600 })\n// after\nconst { BoxClipRectangle } = require('selenium-webdriver/bidi/clipRectangle')\nparams.clipRectangle(new BoxClipRectangle(0, 0, 800, 600))","handlingStrategy":"type-guard","validationCode":"const { BoxClipRectangle, ElementClipRectangle } = require('selenium-webdriver/bidi/clipRectangle')\nconst ok = clip instanceof BoxClipRectangle || clip instanceof ElementClipRectangle\nif (ok) params.clipRectangle(clip)","typeGuard":"function isClipRectangle(v) {\n  const { BoxClipRectangle, ElementClipRectangle } = require('selenium-webdriver/bidi/clipRectangle')\n  return v instanceof BoxClipRectangle || v instanceof ElementClipRectangle\n}","tryCatchPattern":null,"preventionTips":["Always instantiate clip rectangles via their constructors, never inline objects","Remember only BoxClipRectangle and ElementClipRectangle are accepted — not the base ClipRectangle"],"tags":["bidi","screenshot","validation","clip-rectangle","type-check"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}