SeleniumHQ/selenium · error · TypeError

#{type.inspect} is not a valid key subtype

Error message

#{type.inspect} is not a valid key subtype

What it means

Raised as TypeError by TypingInteraction#assert_type when the type passed to TypingInteraction.new(source, type, key) is not a key in KeyInput::SUBTYPES. SUBTYPES permits exactly :down (→ :keyDown), :up (→ :keyUp), and :pause (→ :pause). Any other Symbol or value is rejected at construction time.

Source

Thrown at rb/lib/selenium/webdriver/common/interactions/typing_interaction.rb:43

      #
      # @api private
      #

      class TypingInteraction < Interaction
        attr_reader :type

        def initialize(source, type, key)
          super(source)
          @type = assert_type(type)
          @key = Keys.encode_key(key)
        end

        def assert_source(source)
          raise TypeError, "#{source.type} is not a valid input type" unless source.is_a? KeyInput
        end

        def assert_type(type)
          raise TypeError, "#{type.inspect} is not a valid key subtype" unless KeyInput::SUBTYPES.key? type

          KeyInput::SUBTYPES[type]
        end

        def encode
          {type: @type, value: @key}
        end
      end # TypingInteraction
    end # Interactions
  end # WebDriver
end # Selenium

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Use key_input.create_key_down(key) / create_key_up(key) so the type is set correctly.
  2. If constructing TypingInteraction directly, pass exactly :down, :up, or :pause.

Example fix

# before
act = Selenium::WebDriver::Interactions::TypingInteraction.new(key_input, :press, 'a') # => TypeError

# after
act = Selenium::WebDriver::Interactions::TypingInteraction.new(key_input, :down, 'a')
Defensive patterns

Strategy: validation

Validate before calling

SUBTYPES = Selenium::WebDriver::Interactions::KeyInput::SUBTYPES
type = type.to_sym if type.is_a?(String)
raise "unsupported key subtype: #{type}" unless SUBTYPES.key?(type)

Type guard

def valid_key_subtype?(t)
  Selenium::WebDriver::Interactions::KeyInput::SUBTYPES.key?(t.is_a?(Symbol) ? t : t.to_sym)
end

Prevention

When it happens

Trigger: Constructing TypingInteraction.new(key_input, :press, 'a'), :type, 'keydown' (String), or :keystroke. Rarely user-facing because KeyInput#create_key_down / create_key_up hard-code :down/:up; only hit by direct TypingInteraction construction.

Common situations: Custom action-builder code or a port from another bindings language whose key-action names differ; passing a String instead of a Symbol.

Related errors


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