SeleniumHQ/selenium · error · Error

Value must be an instance of BytesValue. Received:'${value}'

Error message

Value must be an instance of BytesValue. Received:'${value}'

What it means

Thrown by the PartialCookie constructor (partialCookie.js) when the value argument is not an instance of BytesValue. The WebDriver BiDi cookie value must be a BytesValue (type+value), and the constructor serializes it via Object.fromEntries(value.asMap()), so the instance type is enforced. A raw string or object fails the instanceof guard.

Source

Thrown at javascript/selenium-webdriver/bidi/partialCookie.js:38

/**
 * Represents a partial cookie used to set cookies.
 * Described in https://w3c.github.io/webdriver-bidi/#command-storage-setCookie.
 * @class
 */
class PartialCookie {
  #map = new Map()

  /**
   * Represents a partial cookie.
   * @class
   * @param {string} name - The name of the cookie.
   * @param {BytesValue} value - The value of the cookie as an instance of BytesValue.
   * @param {string} domain - The domain of the cookie.
   */
  constructor(name, value, domain) {
    this.#map.set('name', name)
    if (!(value instanceof BytesValue)) {
      throw new Error(`Value must be an instance of BytesValue. Received:'${value}'`)
    }
    this.#map.set('value', Object.fromEntries(value.asMap()))
    this.#map.set('domain', domain)
  }

  /**
   * Sets the path for the cookie.
   *
   * @param {string} path - The path for the cookie.
   * @returns {PartialCookie} - The updated PartialCookie instance for chaining.
   */
  path(path) {
    this.#map.set('path', path)
    return this
  }

  /**
   * Sets the size of the cookie.

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Wrap the cookie value in a BytesValue: new PartialCookie('name', new BytesValue(BytesValue.Type.STRING, 'val'), 'example.com').
  2. For binary cookie values, use BytesValue.Type.BASE64 with a base64 string.
  3. If migrating from a classic cookie object, convert cookie.value into new BytesValue(cookie.value.type || BytesValue.Type.STRING, cookie.value.value || cookie.value).

Example fix

// before
const cookie = new PartialCookie('session', 'abc123', 'example.com')

// after
const { BytesValue } = require('selenium-webdriver/bidi/networkTypes')
const cookie = new PartialCookie('session', new BytesValue(BytesValue.Type.STRING, 'abc123'), 'example.com')
Defensive patterns

Strategy: type-guard

Validate before calling

function toCookieValue(v) {
  if (v instanceof BytesValue) return v
  if (typeof v === 'string') return new BytesValue(BytesValue.Type.STRING, v)
  throw new TypeError('cookie value must be BytesValue or string')
}

Type guard

function isBytesValue(v) {
  return v instanceof BytesValue
}

Prevention

When it happens

Trigger: Calling new PartialCookie('name', 'val', 'example.com') (raw string value), new PartialCookie('name', 123, 'example.com'), or new PartialCookie('name', {type:'string',value:'val'}, 'example.com').

Common situations: Coming from the classic cookies API where values are plain strings; building a cookie from parsed JSON; forgetting that the BiDi cookie value is a BytesValue, not a string.

Related errors


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