selenium.common.exceptions.WebDriverException: Message: invalid session id in Python

selenium.common.exceptions.WebDriverException: Message: invalid session id is a Python WebDriver error which occurs when the server cannot recognize the session identifier.

In this article I am going to try and solve the error while I try to explain why the error is actually popping up, we will also introduce some solutions that worked for other developers and see if those solutions can solve the error in your particular situation.

Describing selenium.common.exceptions.WebDriverException: Message: invalid session id in Python

This error is a WebDriver error, the server just cannot recognize the session id. There are two reason why this can occur, either the id of the session is incorrect or the id has been deleted, sometimes that happens by mistake.

The error can happen when you are making a browser automation script using Selenium, the message of the error will look like something like this.

                                                                       #
Traceback (most recent call last):
    browser.get(url)
    dirk_price = get_price(dirk_url, dirk_classname)
...
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid session id
                                                                       #

Bellow is a number of tested solutions that I have tried and worked for me.

The Main Solution

The first reason this error can occur is when the browser crashes.

So how do you know if the browser crashing caused the error?

Easy, you can check the error code, if it says something that matches the line bellow, then the session id has been deleted because of the browser crashing.

                                                                       #
selenium.common.exceptions.WebDriverException: Message: unknown error: session deleted because of page crash
from tab crashed
                                                                       #

This is a nested case so if you want to know how to solve this problem I invite you to visit the page bellow.

                                                                       #
https://github.com/chromium/chromium/blob/d7da0240cae77824d1eda25745c4022757499131/chrome/test/chromedriver/commands.cc#L265
                                                                       #

Using CTRL + F for the element session deleted because of page crash inside the page.

Solution 2 : change the format of the URL Selenium uses

Sometimes, its enough to split test the actual URL format of the page you are working on. For example you are scraping or automating some process related to twitch.

When the error first happened you used

                                                                       #
driver.get('https://twitch.com')
                                                                       #

You can run the same script but after changing the format of the URL.

                                                                       #
driver.get('twitch.com')
                                                                       #

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

That is it guys, this is the end of this article / guide, I hope you found it useful in solving your particular problem, you can support our work on our Kofi account, you do not have to but you can donate if you want.

Thank you for reading, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/