SeleniumHQ/selenium · error · InvalidArgumentError

key is not a string: ${key}

Error message

key is not a string: ${key}

What it means

Thrown by checkCodePoint() in input.js when the key argument is neither a number nor a string. This function validates keyboard input for BiDi/classic actions, requiring each key to be a single Unicode code point. Numbers are converted via String.fromCodePoint; non-string, non-number values are rejected.

Source

Thrown at javascript/selenium-webdriver/lib/input.js:253

Device.Type = {
  KEY: 'key',
  NONE: 'none',
  POINTER: 'pointer',
  WHEEL: 'wheel',
}

/**
 * @param {(string|Key|number)} key
 * @return {string}
 * @throws {!(InvalidArgumentError|RangeError)}
 */
function checkCodePoint(key) {
  if (typeof key === 'number') {
    return String.fromCodePoint(key)
  }

  if (typeof key !== 'string') {
    throw new InvalidArgumentError(`key is not a string: ${key}`)
  }

  key = key.normalize()
  if (Array.from(key).length !== 1) {
    throw new InvalidArgumentError(`key input is not a single code point: ${key}`)
  }
  return key
}

/**
 * Keyboard input device.
 *
 * @final
 * @see <https://www.w3.org/TR/webdriver/#dfn-key-input-source>
 */
class Keyboard extends Device {
  /** @param {string} id the device ID. */
  constructor(id) {

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Use Key constants from selenium-webdriver (Key.ENTER, Key.TAB, etc.) or single-character strings.
  2. Validate the input before use: if (typeof key === 'string' || typeof key === 'number').
  3. Ensure the variable holding the key is initialized and not null/undefined.
  4. Use driver.actions().sendKeys(text) for typing strings, and keyDown/keyUp only for modifier keys.

Example fix

// before
actions.keyDown(someObject) // someObject is not a key
// after
const { Key } = require('selenium-webdriver')
actions.keyDown(Key.ENTER)
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof key !== 'string' && typeof key !== 'number') {
  throw new TypeError(`key must be a string, number, or Key; got ${typeof key}`)
}

Type guard

/**
 * @param {*} k
 * @returns {boolean}
 */
function isValidKeyInput(k) {
  return typeof k === 'string' || typeof k === 'number'
}

Prevention

When it happens

Trigger: Passing null, undefined, an object, an array, or a boolean to keyboard actions (keyDown, keyUp, sendKeys) instead of a string, Key constant, or number. Passing a Key constant incorrectly or a variable that resolved to null.

Common situations: Dynamic key data that is null or undefined; passing a DOM event object instead of a key; using an unexported/undefined Key constant; passing an array of keys where a single key is expected; variable not initialized before use.

Related errors


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