SeleniumHQ/selenium · warning · RuntimeError

Selenium server isn't running

Error message

Selenium server isn't running

What it means

Raised by `stop()` when `self.process is None`, i.e. start() was never called (or stop() was already invoked). Since there is no child process to terminate, the method refuses to act and raises. It is a RuntimeError indicating lifecycle misuse.

Source

Thrown at py/selenium/webdriver/remote/server.py:223

        host = self.host if self.host is not None else "localhost"

        try:
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
                sock.connect((host, self.port))
            raise ConnectionError(f"Selenium server is already running, or something else is using port {self.port}")
        except ConnectionRefusedError:
            print("Starting Selenium server...")
            self.process = subprocess.Popen(command, env=self.env)
            print(f"Selenium server running as process: {self.process.pid}")
            if not self._wait_for_server(timeout=self.startup_timeout):
                raise TimeoutError(f"Timed out waiting for Selenium server at {self.status_url}")
            print("Selenium server is ready")
        return self.process

    def stop(self):
        """Stop the server."""
        if self.process is None:
            raise RuntimeError("Selenium server isn't running")
        else:
            if self.process.poll() is None:
                self.process.terminate()
                self.process.wait()
            self.process = None
            print("Selenium server has been terminated")

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Guard stop() with a check, or only call it when start() succeeded.
  2. Track a started flag and call stop() exactly once in a try/finally.
  3. Catch start() failures so teardown knows whether stop() is valid.

Example fix

# before
server = Server()
# start() failed / wasn't called
server.stop()

# after
server = Server()
started = False
try:
    server.start(); started = True
finally:
    if started:
        server.stop()
Defensive patterns

Strategy: validation

Validate before calling

if server.process is not None:
    server.stop()

Prevention

When it happens

Trigger: Calling stop() before start(); calling stop() twice; or calling stop() after start() raised an exception so `process` was never assigned.

Common situations: Test teardown running unconditionally in tearDown even when setup failed early; a finally block that runs after a skipped/partial start; calling quit/stop in both an explicit cleanup and an atexit hook.

Related errors


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