SeleniumHQ/selenium · error · Error::WebDriverError

invalid port: #{@port}

Error message

invalid port: #{@port}

What it means

Raised by ServiceManager#initialize when the config.port value is less than 1. ServiceManager is the internal class that launches and manages the driver server process. This mirrors the Service#initialize check (error 429); if a Service is constructed with an invalid port and passed to ServiceManager, this guard fires.

Source

Thrown at rb/lib/selenium/webdriver/common/service_manager.rb:47

      START_TIMEOUT = 20
      SOCKET_LOCK_TIMEOUT = 45
      STOP_TIMEOUT = 20

      #
      # End users should use a class method for the desired driver, rather than using this directly.
      #
      # @api private
      #

      def initialize(config)
        @executable_path = config.executable_path
        @host = Platform.localhost
        @port = config.port
        @io = config.log
        @extra_args = config.args
        @shutdown_supported = config.shutdown_supported

        raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1
      end

      def start
        raise "already started: #{uri.inspect} #{@executable_path.inspect}" if process_running?

        Platform.exit_hook { stop } # make sure we don't leave the server running

        socket_lock.locked do
          find_free_port
          start_process
          connect_until_stable
        end
      end

      def stop
        return unless @shutdown_supported
        return if process_exited?

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Do not mutate service.port after construction; set it via the port: keyword in Service.new.
  2. Ensure any custom Service subclass calls super and preserves the port validation.
  3. Validate port before assigning: service.port = valid_port if valid_port > 0.

Example fix

// before
service = Selenium::WebDriver::Service.chrome
service.port = 0  # bypasses initialize guard
service.launch  # ServiceManager.new raises

// after
service = Selenium::WebDriver::Service.chrome(port: 9515)
service.launch
Defensive patterns

Strategy: validation

Validate before calling

# Ensure port is valid before launching service
service = Selenium::WebDriver::Service.chrome(port: 9515)
if service.port.nil? || service.port < 1
  raise "Invalid port configured: #{service.port}"
end
service.launch

Prevention

When it happens

Trigger: A Service object is created with port < 1 but the check in Service is somehow bypassed (e.g., by directly mutating attr_accessor :port before launch). A custom config object passed to ServiceManager with port set to 0 or negative. Subclass overriding initialize without calling super properly.

Common situations: Directly assigning service.port = 0 after construction (the attr_accessor bypasses the initialize guard). Custom Service subclass that overrides initialize and sets @port incorrectly. Programmatic port manipulation between Service creation and ServiceManager.new.

Related errors


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