Selenium: InvalidSessionIdException: invalid session id
Last verified
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#invalidsessionidexceptionWhat it means
Every WebDriver command carries a session ID. This exception means the driver no longer recognizes that ID, because the session has ended. Sessions end when you call driver.quit(), when the last browser window is closed with driver.close(), or when the browser crashes or is killed. Any command after that, including a cleanup quit() in some bindings, fails with this message. Newer ChromeDriver versions may add detail such as session deleted as the browser has closed the connection.
In test suites it often points to a shared driver that one test quit while others still use it, or to a browser crash under memory pressure that only shows up as the next command failing. On Selenium Grid, sessions also end when they exceed the node's idle timeout.
What is not wrong: the element or page logic of the failing line. The browser behind the session is gone.
What it looks like
import pytest
from selenium import webdriver
@pytest.fixture
def driver():
d = webdriver.Chrome()
yield d
d.quit() # one driver per test, quit exactly once
def test_title(driver):
driver.get("https://example.com/")
assert "Example" in driver.titleWhy it happens
- driver.quit() ran earlier, for example in a teardown shared between tests.
- driver.close() closed the last remaining window, which ends the session.
- The browser crashed (out of memory, /dev/shm exhaustion in Docker).
- A Selenium Grid node ended the session after its idle timeout.
- Parallel tests share a single driver instance.
How to fix it
- Search for quit() and close() calls and make sure nothing runs after them.
- Give each test or worker its own driver (function-scoped fixture or per-thread driver).
- In containers, add --disable-dev-shm-usage or increase shared memory to prevent crashes.
- On Grid, raise the session idle timeout or keep long-running sessions active.
- Use driver.close() only when another window remains; use driver.quit() to end the session.
How to stop it recurring
Tie each driver's lifetime to one owner, a fixture or a context manager, and never share drivers across threads. Monitor container memory, because browser crashes surface only as this exception on the next command. Keep Grid timeouts aligned with the longest step in your flows.
Official documentation: www.selenium.dev/documentation/webdriver/troubleshooting/errors/#invalidsessionidexception. See all Selenium error messages.
Related errors
Definitions
- SeleniumSelenium is an open-source browser automation project, best known for Selenium WebDriver, a library that contr
- ChromeDriverChromeDriver is the Chromium project's standalone server that implements the W3C WebDriver standard for Chrome
- CSS SelectorA CSS selector is a pattern used to identify and target specific HTML elements on a web page, widely used in b
- Headless ChromeHeadless Chrome is Google Chrome running without a visible window. It renders pages and runs JavaScript like n
- IframeAn iframe (inline frame) is an HTML element that embeds a separate web page within a parent page, creating an
- Web ElementA web element is any individual component rendered on a web page — buttons, links, input fields, images, text
Quick answers
What does "InvalidSessionIdException: invalid session id" mean in Selenium?
Every WebDriver command carries a session ID. This exception means the driver no longer recognizes that ID, because the session has ended. Sessions end when you call driver.quit() , when the last browser window is closed with driver.close() , or when the browser crashes or is killed. Any command after that, including a cleanup quit() in some bindings, fails with this message.
How do I fix "InvalidSessionIdException: invalid session id"?
1. Search for quit() and close() calls and make sure nothing runs after them. 2. Give each test or worker its own driver (function-scoped fixture or per-thread driver). 3. In containers, add --disable-dev-shm-usage or increase shared memory to prevent crashes. 4. On Grid, raise the session idle timeout or keep long-running sessions active. 5. Use driver.close() only when another window remains; use driver.quit() to end the session.
Run browser automation you can watch and take over
Autonoly runs each job in a real cloud Chromium browser driven by Playwright. Timeouts and detached elements still happen; the difference is that you can watch the run live, see the step that failed, and take control of the browser to finish it.
Also see Live browser control.