SeleniumHQ/selenium · error · Error
Pattern must be an instance of UrlPattern. Received:'${patte
Error message
Pattern must be an instance of UrlPattern. Received:'${pattern}' What it means
AddInterceptParameters.urlPatterns() iterates an array and validates each entry is an instance of UrlPattern. This is the batch variant of urlPattern(): any non-UrlPattern element in the array throws. The error message is slightly different from the single-pattern variant (no stray ')'), confirming these are two distinct throw sites.
Source
Thrown at javascript/selenium-webdriver/bidi/addInterceptParameters.js:57
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()))
})
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}'`)
}
View on GitHub (pinned to aa36b38e69)
Solutions
- Ensure every array element is a UrlPattern instance built via new UrlPattern().
- Separate structured patterns (urlPatterns) from string URLs (urlStringPatterns) into the correct method.
- Dedupe node_modules so instanceof resolves against one UrlPattern class.
- Filter out null/undefined entries before calling.
Example fix
// before
params.urlPatterns([
{ protocol: 'https', hostname: 'a.com' },
{ protocol: 'https', hostname: 'b.com' },
])
// after
const mk = (h) => new UrlPattern().protocol('https').hostname(h)
params.urlPatterns([mk('a.com'), mk('b.com')]) Defensive patterns
Strategy: validation
Validate before calling
if (!Array.isArray(patterns)) throw new TypeError('urlPatterns expects an array')
patterns.forEach((p) => {
if (!(p instanceof UrlPattern)) throw new TypeError('Each pattern must be a UrlPattern')
}) Type guard
function areUrlPatterns(arr) {
return Array.isArray(arr) && arr.every((p) => p instanceof UrlPattern)
} Prevention
- Build every array element via new UrlPattern().
- Separate structured patterns (urlPatterns) from strings (urlStringPatterns).
- Dedupe node_modules to avoid realm instanceof failures.
- Filter null/undefined entries before calling.
When it happens
Trigger: Calling urlPatterns([{ protocol: 'https' }, ...]) with plain objects. Mixing UrlPattern instances and strings in the array (strings should go via urlStringPatterns). Passing an array containing null/undefined. Cross-realm UrlPattern instances where instanceof fails.
Common situations: Building the array from user/config data as plain objects. Mixing pattern types in one call instead of separating structured vs string patterns. Duplicate selenium-webdriver installs causing instanceof realm mismatch.
Related errors
- Pattern must be an instance of UrlPattern. Received: '${patt
- 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/4b80cae65fdbe775.
Report an issue: GitHub.