24

I'm trying to push cookies to selenium firefox webdriver stored from previous session, but I got error:

org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse

I read this HTML Standard Cookie-averse and understand nothing at all.

So, question is how to push cookies to webdriver session stored from previous one?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user3534746
  • 653
  • 1
  • 5
  • 7

3 Answers3

31

Thank you DebanjanB! I've tried to push cookies just after driver start and before open URL tab.

Working solution:

driver.get('http://mydomain')
driver.manage.addCookie(....)
driver.get('http://mydomain')

Just open a tab, add cookie and reopen a tab again

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
user3534746
  • 653
  • 1
  • 5
  • 7
26

This error message...

org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse

...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 Specification 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 when you visit a cookie-averse document, such as a file on your local disk.

As an example:

  • Sample Code:
from selenium import webdriver
from selenium.common import exceptions

session = webdriver.Firefox()
session.get("file:///home/jdoe/document.html")
try:
    foo_cookie = {"name": "foo", "value": "bar"}
    session.add_cookie(foo_cookie)
except exceptions.InvalidCookieDomainException as e:
    print(e.message)
        
  • Console Output:

    InvalidCookieDomainException: Document is cookie-averse
    

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 had 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')

Reference

You can find a detailed discussion in:

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I guess your situation is you set cookie with driver.manage.addCookie(....) before you get the url with driver.get('http://mydomain').

Cookie can be only add to the request with same domain.
When webdriver init, it's request url is `data:` so you cannot add cookie to it.
So first make a request to your url then add cookie, then request you url again.
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 1
    What if you cant make request without cookie? Usually your browser provides already stored cookis during first request on subsequent sessions. Unfortunally browser uses a temporary profile so cookies are not saved. – Merilix2 Feb 18 '22 at 19:25
  • Well, something like `driver.Url = "https:///robots.txt";` may be possible in most cases. – Merilix2 Feb 18 '22 at 20:41