[Fixed]-Selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain while executing tests in Django with Selenium


In selenium we must use the URL of the testing server by default this is localhost, to enable access to an external accessible server address which is the hosting docker container’s address we need to use the server machine’s ip address,
So set as below,

class BaseTestCase(StaticLiveServerTestCase):
Provides base test class which connects to the Docker
container running Selenium.
host = ''  # Bind to to allow external access

def setUpClass(cls):
    # Set host to externally accessible web server address
    cls.host = socket.gethostbyname(socket.gethostname())

reference here


This error message…

selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain

…implies that an illegal attempt was made to set a cookie under a different domain than that of the current document.


As per the HTML-Living Standard Specs a Document Object may be categorized as a cookie-averse Document object in the following circumstances :

  • A Document that has no Browsing Context.
  • A Document whose URL’s scheme is not a network scheme.

Deep Dive

As per Invalid cookie domain this error may occur if the current domain were to be example.com, it would not be possible to add the cookie for the domain example.org.

As an example:

  • Sample Code:

    from selenium import webdriver
    from selenium.common import exceptions
    session = webdriver.Firefox()
        cookie = {"name": "foo",
              "value": "bar",
              "domain": "example.org"}
    except exceptions.InvalidCookieDomainException as e:
  • Console Output:

    InvalidCookieDomainException: https://example.org/


If you have stored the cookie from domain example.com, these stored cookies can’t be pushed through the webdriver session to any other different domanin e.g. example.edu. The stored cookies can be used only within example.com. Further, to automatically login an user in future, you need to store the cookies only once, and that’s when the user have logged in. Before adding back the cookies you need to browse to the same domain from where the cookies were collected.


As an example, you can store the cookies once the user havd logged in within an application as follows:

from selenium import webdriver
import pickle

driver = webdriver.Chrome()

# storing the cookies
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

Later if you want the user automatically logged-in, you need to browse to the specific domain /url first and then you have to add the cookies as follows:

from selenium import webdriver
import pickle

driver = webdriver.Chrome()

# loading the stored cookies
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    # adding the cookies to the session through webdriver instance

Additional Consideration

It seems you are using chrome=77.0.3865.75. Ideally you need to ensure that:


You can find a detailed discussion in:


I run into a similar problem while working with C#. I solved it by first setting the URL to the home page that does not need a user session and then setting the cookies and after that opening the page requiring a session. Here is the code

webDriver = new ChromeDriver(".");
        String f = @"./scrapper/cookies.data";
        String[] lines = File.ReadAllLines(f);
        webDriver.Url = "https://myhomepage.com/";
        foreach (String ln in lines)
            String[] temp = ln.Split(';');
            String name = temp[0];
            String value = temp[1];
            String domain = temp[2];
            String path = temp[3];
            DateTime expiry = DateTime.Now.AddDays(60);
            if (!String.IsNullOrEmpty(temp[4]))
                expiry = DateTime.Parse(temp[4]);
            Boolean isSecure = Boolean.Parse(temp[5]);
            Cookie cookie = new Cookie(name, value, domain, path, expiry);

        webDriver.Url = "https://target_page_requiring_session.php";

        IWebElement tab3 = webDriver.FindElement(By.XPath("//*[@id='TabIDS_3']"));


I run into the same problem. My solution was simply to avoid setting domain value (wich is optional, by the way) in the cookie.

    class MyTestCase(StaticLiveServerTestCase):
        host = os.environ['DJANGO_CONTAINER_NAME']

        def test_logged_in(self):
            self.client.login(username="integration", password="test")
            self.selenium.add_cookie({"name": settings.SESSION_COOKIE_NAME, 
                                    "value": self.client.cookies[settings.SESSION_COOKIE_NAME].value})
            post = Post.objects.first()
            self.selenium.get(f"{self.live_server_url}{reverse('post', args=(post.slug, ))}")

The main point here is to let the Django client (here self.client) login and then copy its session cookie to a Selenium browser session, which should have already visited a page from your live server – in other words, first let the Selenium browser open a page, then set the cookie (skipping domain) and then, the selenium browser is logged in.


Leave a comment