SeleniumHQ/selenium · error · Error

Pattern must be an instance of String. Received:'${pattern}'

Error message

Pattern must be an instance of String. Received:'${pattern}'

What it means

AddInterceptParameters.urlStringPattern() validates that the single pattern argument is a JS string (typeof === 'string'), then pushes it as { type: 'string', pattern }. This is the simpler alternative to UrlPattern for whole-string URL matching. Passing a UrlPattern instance, a number, an object, null, or undefined fails the typeof check.

Source

Thrown at javascript/selenium-webdriver/bidi/addInterceptParameters.js:73

    patterns.forEach((pattern) => {
      if (!(pattern instanceof UrlPattern)) {
        throw new Error(`Pattern must be an instance of UrlPattern. Received:'${pattern}'`)
      }
      this.#urlPatterns.push(Object.fromEntries(pattern.asMap()))
    })
    return this
  }

  /**
   * Adds string URL to intercept.
   *
   * @param {string} pattern - The URL pattern to be added.
   * @returns {AddInterceptParameters} - Returns the instance of AddInterceptParameters for chaining..
   * @throws {Error} - If the pattern is not an instance of String.
   */
  urlStringPattern(pattern) {
    if (typeof pattern !== 'string') {
      throw new Error(`Pattern must be an instance of String. Received:'${pattern}'`)
    }

    this.#urlPatterns.push({ type: 'string', pattern: pattern })
    return this
  }

  /**
   * Adds array of string URLs to intercept.
   * @param {string[]} patterns - An array of URL string patterns.
   * @returns {this} - Returns the instance of AddInterceptParameters for chaining.
   */
  urlStringPatterns(patterns) {
    patterns.forEach((pattern) => {
      if (typeof pattern !== 'string') {
        throw new Error(`Pattern must be an instance of String. Received:'${pattern}'`)
      }
      this.#urlPatterns.push({ type: 'string', pattern: pattern })
    })

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Pass a primitive string: urlStringPattern('https://example.com/path').
  2. If you have a structured pattern, use urlPattern(new UrlPattern()...) instead.
  3. Convert URL objects with .toString()/.href before passing.
  4. Avoid new String(...) wrappers; use string literals/primitives.

Example fix

// before
params.urlStringPattern(new UrlPattern().hostname('example.com'))

// after
params.urlStringPattern('https://example.com/*')
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof pattern !== 'string') {
  throw new TypeError('urlStringPattern expects a primitive string')
}

Type guard

function isStringPattern(p) {
  return typeof p === 'string'
}

Prevention

When it happens

Trigger: Calling urlStringPattern(new UrlPattern()) (wrong method — should be urlPattern). Passing a non-string (number, object). Passing null/undefined. Passing a String object (new String('x')) — note typeof is 'object', so it would fail despite being string-like.

Common situations: Confusing urlStringPattern (plain string) with urlPattern (structured UrlPattern). Passing URL objects from the `url` package (typeof 'object'). Forgetting to call .toString() on a value before passing.

Related errors


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