SeleniumHQ/selenium · error · ArgumentError

Button number cannot be negative!

Error message

Button number cannot be negative!

What it means

Raised as ArgumentError by PointerPress#assert_button when button is an Integer but negative (button.negative? is true). Non-negative integers are accepted verbatim as the W3C button code (0=left/primary, 1=middle/auxiliary, 2=right/secondary, 3=back, 4=forward).

Source

Thrown at rb/lib/selenium/webdriver/common/interactions/pointer_press.rb:69

        def encode
          process_opts.merge('type' => type.to_s, 'button' => @button)
        end

        private

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

        def assert_button(button)
          case button
          when Symbol
            raise ArgumentError, "#{button} is not a valid button!" unless BUTTONS.key? button

            BUTTONS[button]
          when Integer
            raise ArgumentError, 'Button number cannot be negative!' if button.negative?

            button
          else
            raise TypeError, "button must be a positive integer or one of #{BUTTONS.keys}, not #{button.class}"
          end
        end

        def assert_direction(direction)
          raise ArgumentError, "#{direction.inspect} is not a valid button direction" unless DIRECTIONS.key? direction

          DIRECTIONS[direction]
        end
      end # PointerPress
    end # Interactions
  end # WebDriver
end # Selenium

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Pass a non-negative integer (0..4 are the standard W3C codes).
  2. Guard computed values: button = [computed, 0].max before passing.
  3. Prefer named Symbols (:left/:middle/:right) when the button is one of the standard ones.

Example fix

# before
driver.action.pointer_down(idx - 1).perform # idx == 0 => ArgumentError: Button number cannot be negative!

# after
driver.action.pointer_down([idx - 1, 0].max).perform
Defensive patterns

Strategy: validation

Validate before calling

button = [button, 0].max if button.is_a?(Integer)
driver.action.pointer_down(button).perform

Prevention

When it happens

Trigger: Calling pointer_down/pointer_up (or PointerPress.new) with a negative integer such as pointer_down(-1), or computed button = some_index - 1 that can go below zero.

Common situations: Off-by-one in computed button indices; passing a sentinel like -1 to mean 'no button'; decrementing an index without a floor check.

Related errors


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