SeleniumHQ/selenium · warning · WebDriverException

Port number must be in the range 1..65535

Error message

Port number must be in the range 1..65535

What it means

Raised by the (deprecated) FirefoxProfile.port setter when the int port value falls outside the valid TCP range 1..65535. It fires only after the isinstance(port, int) check passes and int(port) succeeds, so the value is a valid integer but numerically out of range (0, negative, or > 65535).

Source

Thrown at py/selenium/webdriver/firefox/firefox_profile.py:117

        """Gets the profile directory that is currently being used."""
        return self._profile_dir

    @property
    @deprecated("The port is stored in the Service class")
    def port(self):
        """Gets the port that WebDriver is working on."""
        return self._port

    @port.setter
    @deprecated("The port is stored in the Service class")
    def port(self, port) -> None:
        """Sets the port that WebDriver will be running on."""
        if not isinstance(port, int):
            raise WebDriverException("Port needs to be an integer")
        try:
            port = int(port)
            if port < 1 or port > 65535:
                raise WebDriverException("Port number must be in the range 1..65535")
        except (ValueError, TypeError):
            raise WebDriverException("Port needs to be an integer")
        self._port = port
        self.set_preference("webdriver_firefox_port", self._port)

    @property
    @deprecated("Allowing untrusted certs is toggled in the Options class")
    def accept_untrusted_certs(self):
        return self._desired_preferences["webdriver_accept_untrusted_certs"]

    @accept_untrusted_certs.setter
    @deprecated("Allowing untrusted certs is toggled in the Options class")
    def accept_untrusted_certs(self, value) -> None:
        if not isinstance(value, bool):
            raise WebDriverException("Please pass in a Boolean to this call")
        self.set_preference("webdriver_accept_untrusted_certs", value)

    @property

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Set the port on the Firefox Service, which supports 0 for OS-assigned: Service(port=0).
  2. If using the deprecated profile.port, supply a value strictly within 1..65535.
  3. Validate the value before assignment if it comes from external config.

Example fix

# before (deprecated, out of range)
profile.port = 0  # -> WebDriverException: Port number must be in the range 1..65535

# after — let the Service handle port allocation
from selenium.webdriver.firefox.service import Service
service = Service(port=0)  # OS picks a free port
driver = webdriver.Firefox(service=service)
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(port, int) and 1 <= port <= 65535, 'port must be in 1..65535'
profile.port = port

Type guard

def is_valid_port(v) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and 1 <= v <= 65535

Try / catch

from selenium.common.exceptions import WebDriverException
try:
    profile.port = raw
except WebDriverException:
    raw = max(1, min(65535, int(raw)))
    profile.port = raw

Prevention

When it happens

Trigger: Assigning profile.port = 0, profile.port = -1, or profile.port = 70000. Also a bool True (which is int 1) passes range, but False (int 0) fails it.

Common situations: Using port 0 expecting 'OS chooses' (the profile setter does not support that semantics, unlike the Service), passing an out-of-range value from misparsed config, or copying a port number with a typo.

Related errors


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