selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain while executing tests in Django with Selenium

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.


Details

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()
    session.get("https://example.com/")
    try:
        cookie = {"name": "foo",
              "value": "bar",
              "domain": "example.org"}
        session.add_cookie(cookie)
    except exceptions.InvalidCookieDomainException as e:
        print(e.message)
    
  • Console Output:

    InvalidCookieDomainException: https://example.org/
    

Solution

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.


Example

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()
driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')
driver.find_element_by_name("username").send_keys("abc123")
driver.find_element_by_name("password").send_keys("123xyz")
driver.find_element_by_name("submit").click()

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

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()
driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')

# loading the stored cookies
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    # adding the cookies to the session through webdriver instance
    driver.add_cookie(cookie)
driver.get('http://demo.guru99.com/test/cookie/selenium_cookie.php')

Additional Consideration

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


Reference

You can find a detailed discussion in:

Leave a Comment