SeleniumHQ/selenium · error · Error

UserContext must be string. Received:'${userContext}'

Error message

UserContext must be string. Received:'${userContext}'

What it means

Thrown by `CreateContextParameters.userContext()` when the argument is not a string (`typeof userContext !== 'string'`). The user context must be a string id.

Source

Thrown at javascript/selenium-webdriver/bidi/createContextParameters.js:62

   * @throws {Error} - If the background parameter is not a boolean.
   */
  background(background) {
    if (typeof background !== 'boolean') {
      throw new Error(`Background must be boolean. Received:'${background}'`)
    }
    this.#map.set('background', background)
    return this
  }

  /**
   * Sets the user context.
   * @param {string} userContext - The user context to set.
   * @returns {CreateContextParameters} - The updated instance of CreateContextParameters for chaining.
   * @throws {Error} - If the userContext parameter is not a string.
   */
  userContext(userContext) {
    if (typeof userContext !== 'string') {
      throw new Error(`UserContext must be string. Received:'${userContext}'`)
    }
    this.#map.set('userContext', userContext)
    return this
  }

  asMap() {
    return this.#map
  }
}

module.exports = { CreateContextParameters }

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Pass a string user-context id
  2. If you do not need a user context, omit the call entirely — it is optional

Example fix

// before
params.userContext()
// after
params.userContext('default') // or omit the call
Defensive patterns

Strategy: validation

Validate before calling

if (typeof ctx === 'string') params.userContext(ctx)

Type guard

const isUserContext = (v) => typeof v === 'string'

Prevention

When it happens

Trigger: Calling `params.userContext(undefined)`, `params.userContext(0)`, or passing an object.

Common situations: Undefined user context when none was intended (it is optional); passing a numeric or object value.

Related errors


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