SeleniumHQ/selenium · error · Error
Pattern must be an instance of UrlPattern. Received: '${patt
Error message
Pattern must be an instance of UrlPattern. Received: '${pattern})' What it means
AddInterceptParameters.urlPattern() (WebDriver BiDi network.addIntercept) validates that the single pattern argument is an instance of the UrlPattern class from ./urlPattern. UrlPattern is the only structured-pattern type in BiDi network interception (alongside plain string patterns handled by urlStringPattern). Passing a plain object, a string, a Map, or an instance from a different module realm fails the instanceof check. The error message interpolates the received value — note the literal has a stray ')' producing `Received: '<value>)'`, a minor message bug.
Source
Thrown at javascript/selenium-webdriver/bidi/addInterceptParameters.js:41
constructor(phases) {
if (phases instanceof Array) {
phases.forEach((phase) => this.#phases.push(phase))
} else {
this.#phases.push(phases)
}
}
/**
* Adds a URL pattern to intercept.
*
* @param {UrlPattern} pattern - The URL pattern to add.
* @returns {AddInterceptParameters} - Returns the current instance of the class AddInterceptParameters for chaining.
* @throws {Error} - Throws an error if the pattern is not an instance of UrlPattern.
*/
urlPattern(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 array of URL patterns to intercept.
*
* @param {UrlPattern[]} patterns - An array of UrlPattern instances representing the URL patterns to intercept.
* @returns {AddInterceptParameters} - Returns the instance of AddInterceptParameters for chaining.
* @throws {Error} - Throws an error if the pattern is not an instance of UrlPattern.
*/
urlPatterns(patterns) {
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()))View on GitHub (pinned to aa36b38e69)
Solutions
- Construct a UrlPattern via new UrlPattern() and chain .protocol()/.hostname()/.pathname() etc. before passing it to urlPattern().
- If you only have a URL string, use urlStringPattern(url) or urlStringPatterns([urls]) instead.
- Ensure a single copy of selenium-webdriver is installed (dedupe node_modules) so instanceof works.
- Do not pass plain objects or Map instances; only UrlPattern instances are accepted.
Example fix
// before
params.urlPattern({ protocol: 'https', hostname: 'example.com' })
// after
const { UrlPattern } = require('selenium-webdriver/bidi/urlPattern')
const pattern = new UrlPattern().protocol('https').hostname('example.com')
params.urlPattern(pattern) Defensive patterns
Strategy: type-guard
Validate before calling
if (!(pattern instanceof UrlPattern)) {
throw new TypeError('urlPattern expects a UrlPattern instance')
} Type guard
function isUrlPattern(p) {
return p instanceof UrlPattern
} Prevention
- Always construct UrlPattern via new UrlPattern().
- Use urlStringPattern() for plain string URLs.
- Dedupe node_modules so instanceof resolves to one class.
- Never pass plain objects or Maps.
When it happens
Trigger: Calling addInterceptParameters.urlPattern({ protocol: 'https' }) with a plain object instead of a UrlPattern instance. Passing a string URL (should use urlStringPattern instead). Passing a UrlPattern constructed in a different Node module instance/realm where instanceof fails. Passing null/undefined.
Common situations: Confusing urlPattern (structured) with urlStringPattern (plain string). Copying example code that builds a raw object. Multiple copies of the selenium-webdriver package installed, causing instanceof to fail across realms. Importing UrlPattern from the wrong path.
Related errors
- Pattern must be an instance of UrlPattern. Received:'${patte
- Pattern must be an instance of String. Received:'${pattern}'
- Params must be an instance of AddInterceptParameters. Receiv
- Params must be an instance of ContinueRequestParameters. Rec
- Params must be an instance of ContinueResponseParameters. Re
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/89bde9803f532e4f.
Report an issue: GitHub.