Skip to main content
SeleniumDecoded

InvalidSessionIdException

InvalidSessionIdException: Causes and Fixes

Fix Selenium InvalidSessionIdException (NoSuchSessionException in Java, NoSuchSessionError in JS): quit drivers, crashed browsers and Grid session timeouts.

Common W3C: invalid session id Verified against Selenium 4.52.0
Exception name by language
Java org.openqa.selenium.NoSuchSessionException
Python selenium.common.exceptions.InvalidSessionIdException
JavaScript NoSuchSessionError
C# OpenQA.Selenium.WebDriverException

What the error means

Every WebDriver command carries a session ID. “Invalid session id” means the driver or Grid no longer recognises that session: it was ended, the browser died, or the Grid reaped it. Your client object still exists, but the browser behind it is gone.

This error is never fixed by retrying the command. The session is over. The question is who ended it and why.

Bindings name it differently. The W3C code is invalid session id; Java maps it to NoSuchSessionException, Python to InvalidSessionIdException, JavaScript to NoSuchSessionError, and .NET surfaces a WebDriverException whose message contains invalid session id.

What it looks like

// Java
org.openqa.selenium.NoSuchSessionException: invalid session id: session deleted as the browser has closed the connection
from disconnected: not connected to DevTools
(Session info: chrome=131.0.6778.86)
# Python
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
// JavaScript (selenium-webdriver)
NoSuchSessionError: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.
// C#
OpenQA.Selenium.WebDriverException : invalid session id: session deleted because of page crash
from tab crashed
// Grid
org.openqa.selenium.NoSuchSessionException: Unable to find session with ID: 7f3c9b2e4a1d...

The detail after the code is the important part: “session deleted because of page crash” and “browser has closed the connection” point at a crash; “Unable to find session” from a Grid points at a timeout or restart; the JavaScript “did you call quit()” message is the client noticing you reused a quit driver.

Common causes

  1. The driver was already quit. A teardown ran early, an afterEach and an afterAll both call quit(), or a helper closed the last window with close(), which ends the session.
  2. The browser crashed. Out of memory, a tab crash, or a small /dev/shm in Docker taking Chrome down mid-test.
  3. The Grid timed the session out. No command arrived within the node’s session timeout (300 seconds by default), for example during a long debugger pause or a slow non-browser step.
  4. A driver shared between threads or tests. One parallel test quits a driver another test is still using.
  5. The node or container restarted, taking its sessions with it.

How to fix it

The driver was already quit

Make one place in your framework responsible for creating and quitting the driver, and null out the reference after quitting so accidental reuse fails clearly.

Single owner for quit, safe to call twice
Selenium 4 Stable
public final class DriverHolder {
private static final ThreadLocal<WebDriver> DRIVER = new ThreadLocal<>();
public static WebDriver get() { return DRIVER.get(); }
public static void set(WebDriver driver) { DRIVER.set(driver); }
public static void quit() {
WebDriver driver = DRIVER.get();
if (driver != null) {
try { driver.quit(); } finally { DRIVER.remove(); }
}
}
}
import pytest
from selenium import webdriver
@pytest.fixture
def driver():
drv = webdriver.Chrome()
yield drv
drv.quit() # the fixture is the only place that quits
let driver;
beforeEach(async () => {
driver = await new Builder().forBrowser('chrome').build();
});
afterEach(async () => {
if (driver) {
await driver.quit();
driver = undefined;
}
});
public sealed class DriverHolder
{
private static readonly ThreadLocal<IWebDriver> Driver = new();
public static IWebDriver Current => Driver.Value;
public static void Set(IWebDriver driver) => Driver.Value = driver;
public static void Quit()
{
try { Driver.Value?.Quit(); }
finally { Driver.Value = null; }
}
}

Also check for close() on the last remaining window. Closing the final window ends the session in most drivers, so a later command fails with this error. Use quit() for teardown and close() only for extra tabs.

The browser crashed

Look for “page crash”, “tab crashed” or “not connected to DevTools” in the message. In Docker, give the container more shared memory (--shm-size=2g) or add --disable-dev-shm-usage to Chrome options. Reduce memory pressure: fewer parallel browsers per host, headless mode, and closing tabs you no longer need. The official Selenium images set sensible defaults; see Docker and Testcontainers.

The Grid timed the session out

If you pause in a debugger or do slow work between browser commands (database seeding, file processing), the node may reap the session as idle. Either keep idle periods short, move the slow work before session creation, or raise the node’s session timeout (the --session-timeout option on the node, in seconds). Do the same for any cloud provider’s idle timeout capability.

A driver shared between threads or tests

Give each test thread its own driver. Static WebDriver fields shared across parallel tests are the classic cause: one test’s teardown quits the browser under another test. Use ThreadLocal in Java and C#, function-scoped fixtures in pytest, and per-test instances in JavaScript runners. The driver factory and thread safety lesson covers this in depth.

The node or container restarted

Check Grid and container logs around the failure time for restarts, OOM kills or autoscaler scale-downs. In Kubernetes, make sure scale-down waits for running sessions to drain.

How to prevent it

  • One driver per test (or per thread), created and quit in one place.
  • Use quit() in a finally block or fixture teardown, never scattered through test code.
  • Size containers properly and monitor memory; crashes show up as this error.
  • Align Grid session timeouts with how your tests actually behave, including debugging sessions.
  • When this error appears, fail the test immediately and start a fresh session for the next one. Do not try to recover the dead session.

Go deeper