SeleniumHQ/selenium · error · TypeError
Capability keys must be strings: ${typeof key}
Error message
Capability keys must be strings: ${typeof key} What it means
Thrown by Capabilities.set() when the key is not a string (typeof key !== 'string'). All WebDriver capability keys must be strings per the W3C spec. The method enforces this at the type level and rejects numeric, object, symbol, or boolean keys.
Source
Thrown at javascript/selenium-webdriver/lib/capabilities.js:365
/**
* Deletes an entry from this set of capabilities.
*
* @param {string} key the capability key to delete.
*/
delete(key) {
this.map_.delete(key)
}
/**
* @param {string} key The capability key.
* @param {*} value The capability value.
* @return {!Capabilities} A self reference.
* @throws {TypeError} If the `key` is not a string.
*/
set(key, value) {
if (typeof key !== 'string') {
throw new TypeError('Capability keys must be strings: ' + typeof key)
}
this.map_.set(key, value)
return this
}
/**
* Sets whether a WebDriver session should implicitly accept self-signed, or
* other untrusted TLS certificates on navigation.
*
* @param {boolean} accept whether to accept insecure certs.
* @return {!Capabilities} a self reference.
*/
setAcceptInsecureCerts(accept) {
return this.set(Capability.ACCEPT_INSECURE_TLS_CERTS, accept)
}
/**
* @return {boolean} whether the session is configured to accept insecureView on GitHub (pinned to aa36b38e69)
Solutions
- Convert the key to a string: caps.set(String(key), value).
- Validate typeof key === 'string' before calling set().
- Ensure config data sources yield string keys (e.g., via Object.entries on a plain object).
Example fix
// before caps.set(123, 'value') // after caps.set(String(123), 'value')
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof key !== 'string') {
throw new TypeError(`Capability key must be a string, got ${typeof key}`)
}
caps.set(key, value) Type guard
/**
* @param {*} k
* @returns {k is string}
*/
function isStringKey(k) {
return typeof k === 'string'
} Prevention
- Convert keys with String() when sourcing from dynamic data.
- Use Object.entries() on plain objects to guarantee string keys.
- Validate typeof key === 'string' before calling set().
When it happens
Trigger: Calling set(123, value) with a numeric key. Using a key from dynamically parsed data that isn't a string. Passing an object or symbol as a key. Iterating over a structure that yields non-string keys.
Common situations: Using numeric IDs as capability keys; keys derived from JSON with unexpected types; symbol keys from iterators; object references accidentally used as keys.
Related errors
- no capabilities provided for merge
- Params must be an instance of CookieFilter. Received:'${cook
- Port must be a number. Received:'${port}'
- Invalid URL: ${aUrl}
- input must be a string
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/040478aa2ed571ef.
Report an issue: GitHub.