SeleniumHQ/selenium · error · Error

CookieHeader must be an instance of Header. Received:'${head

Error message

CookieHeader must be an instance of Header. Received:'${header}'

What it means

Thrown by ProvideResponseParameters.cookies() when any element of the cookieHeaders array is not an instance of the Header class. The method iterates and serializes each via header.asMap(), so each must be a real Header. A plain object, string, or a PartialCookie is rejected because it lacks the asMap() contract.

Source

Thrown at javascript/selenium-webdriver/bidi/provideResponseParameters.js:58

    if (!(value instanceof BytesValue)) {
      throw new Error(`Value must be an instance of BytesValue. Received: ${typeof value} with value: ${value}`)
    }
    this.#map.set('body', Object.fromEntries(value.asMap()))
    return this
  }

  /**
   * Sets the cookie headers for the response.
   *
   * @param {Header[]} cookieHeaders - An array of cookie headers.
   * @returns {ProvideResponseParameters} - Returns the ProvideResponseParameters object for chaining.
   * @throws {Error} - Throws an error if a cookie header is not an instance of Header.
   */
  cookies(cookieHeaders) {
    const cookies = []
    cookieHeaders.forEach((header) => {
      if (!(header instanceof Header)) {
        throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)
      }
      cookies.push(Object.fromEntries(header.asMap()))
    })

    this.#map.set('cookies', cookies)
    return this
  }

  /**
   * Sets the headers for the response.
   *
   * @param {Header[]} headers - The headers to be set.
   * @returns {ProvideResponseParameters} - Returns the ProvideResponseParameters object for chaining.
   * @throws {Error} - If the provided header is not an instance of Header.
   */
  headers(headers) {
    const headerList = []
    headers.forEach((header) => {

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Build Header instances for each cookie: params.cookies([new Header('Set-Cookie', new BytesValue(BytesValue.Type.STRING, 'a=1; Path=/'))]).
  2. Ensure every element is a Header (instanceof Header), not a PartialCookie or plain object.
  3. If converting from parsed header strings, map each into a new Header with a BytesValue value.

Example fix

// before
const params = new ProvideResponseParameters(id).cookies(['Set-Cookie: a=1'])

// after
const { Header, BytesValue } = require('selenium-webdriver/bidi/networkTypes')
const params = new ProvideResponseParameters(id).cookies([
  new Header('Set-Cookie', new BytesValue(BytesValue.Type.STRING, 'a=1; Path=/')),
])
Defensive patterns

Strategy: type-guard

Validate before calling

const allHeaders = cookieHeaders.every((h) => h instanceof Header)
if (!allHeaders) throw new TypeError('every cookie header must be a Header instance')

Type guard

function isHeaderArray(v) {
  return Array.isArray(v) && v.every((h) => h instanceof Header)
}

Prevention

When it happens

Trigger: Calling params.cookies(['Set-Cookie: a=1']) (raw strings), params.cookies([{name:'a',value:{...}}]) (plain objects), or params.cookies([partialCookieInstance]) (a PartialCookie, not a Header).

Common situations: Passing Set-Cookie header strings parsed from an existing response; reusing PartialCookie objects (which are for storage.setCookie) instead of Headers; deserializing cookie JSON into plain objects.

Related errors


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