SeleniumHQ/selenium · error · Error::WebDriverError

invalid port: #{@port}

Error message

invalid port: #{@port}

What it means

Raised by Selenium::WebDriver::Service#initialize when the provided port, after conversion to Integer, is less than 1. This is a configuration validation: ports must be positive integers in the valid range (1-65535). The constructor calls Integer(port) first (which would raise ArgumentError for non-numeric input), then checks @port < 1.

Source

Thrown at rb/lib/selenium/webdriver/common/service.rb:86

      def initialize(path: nil, port: nil, log: nil, args: nil)
        port ||= self.class::DEFAULT_PORT
        args ||= []

        @executable_path = path
        @host = Platform.localhost
        @port = Integer(port)
        @log = case log
               when :stdout
                 $stdout
               when :stderr
                 $stderr
               else
                 log
               end
        @args = args

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

      def launch
        @executable_path ||= DriverFinder.new(nil, self).driver_path
        ServiceManager.new(self).tap(&:start)
      end

      def shutdown_supported
        self.class::SHUTDOWN_SUPPORTED
      end

      private

      def warn_driver_log_override
        WebDriver.logger.warn('SE_DEBUG is set; overriding user-specified driver logging settings', id: :se_debug)
      end
    end # Service
  end # WebDriver

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Pass a valid port number (1-65535): Selenium::WebDriver::Service.chrome(port: 9515).
  2. Pass nil to use the driver's DEFAULT_PORT constant.
  3. If reading port from an env var, validate it: port = ENV.fetch('PORT', 9515).to_i; raise if port < 1.

Example fix

// before
service = Selenium::WebDriver::Service.chrome(port: 0)
# or accidentally:
port = ENV['DRIVER_PORT'].to_i  # ENV unset => 0
service = Selenium::WebDriver::Service.chrome(port: port)

// after
service = Selenium::WebDriver::Service.chrome(port: 9515)
# or safely from env:
port = ENV.fetch('DRIVER_PORT', 9515).to_i
service = Selenium::WebDriver::Service.chrome(port: port)
Defensive patterns

Strategy: validation

Validate before calling

def valid_port?(port)
  port = Integer(port) rescue nil
  port && port.between?(1, 65535)
end

service = Selenium::WebDriver::Service.chrome(port: valid_port?(my_port) ? my_port : nil)

Prevention

When it happens

Trigger: Passing port: 0 or a negative port number to Service.new. Passing port: nil when the Service subclass has no DEFAULT_PORT constant. Passing a string that converts to 0 or negative via Integer().

Common situations: Passing 0 intending 'auto-assign' (Service does not support this directly; it uses PortProber later). Misconfiguring from environment variables (e.g., ENV['PORT'].to_i when the env var is unset, yielding 0). Custom Service subclass missing the DEFAULT_PORT constant.

Related errors


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