InvalidSessionIdException
InvalidSessionIdException: Causes and Fixes
Fix Selenium InvalidSessionIdException (NoSuchSessionException in Java, NoSuchSessionError in JS): quit drivers, crashed browsers and Grid session timeouts.
| 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
// Javaorg.openqa.selenium.NoSuchSessionException: invalid session id: session deleted as the browser has closed the connectionfrom disconnected: not connected to DevTools (Session info: chrome=131.0.6778.86)# Pythonselenium.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 crashfrom tab crashed// Gridorg.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
- The driver was already quit. A teardown ran early, an
afterEachand anafterAllboth callquit(), or a helper closed the last window withclose(), which ends the session. - The browser crashed. Out of memory, a tab crash, or a small
/dev/shmin Docker taking Chrome down mid-test. - 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.
- A driver shared between threads or tests. One parallel test quits a driver another test is still using.
- 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.
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 pytestfrom selenium import webdriver
@pytest.fixturedef driver(): drv = webdriver.Chrome() yield drv drv.quit() # the fixture is the only place that quitslet 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 afinallyblock 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.
Related errors
- SessionNotCreatedException: the session never started in the first place.
- NoSuchWindowException: one window is gone, but the session is still alive.
- WebDriverException: connection refused and “chrome not reachable” when the driver process itself died.
- TimeoutException: a slow command, not a dead session.
Go deeper
- Driver Factory and Thread Safety Build a WebDriver factory that reads configuration, supports local, Grid and cloud targets, and hands each parallel test its own driver via ThreadLocal or fixtures without leaks.
- Selenium Grid Distribute tests across multiple machines and browsers using Selenium Grid for scalable test execution.
- Docker and Testcontainers Run browsers in the official Selenium Docker images, compose a Grid with video recording, and use Testcontainers to start a disposable browser per test from Java, Python, .NET or Node.
- Parallel Execution Run Selenium tests in parallel to dramatically reduce test execution time.