Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse using Selenium and WebDriver

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?

like image 640
user3534746 Avatar asked Jan 20 '18 03:01

user3534746


People also ask

Can selenium handle cookies?

Selenium WebDriver provides multiple commands for handling the cookies in a website. These Selenium cookies APIs provide you the required mechanism for interacting and querying the cookies. In Selenium Java, these methods are a part of the org. openqa.

What does invalid cookie mean?

The invalid cookie domain error is a WebDriver error that occurs when an illegal attempt was made to set a cookie under a different domain than that of the current document. In WebDriver it is not permissible to set cookies for other domains than the domain of the current browsing context's document's domain.


2 Answers

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

like image 144
user3534746 Avatar answered Sep 17 '22 18:09

user3534746


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:

  • Error when loading cookies into a Python request session
like image 38
undetected Selenium Avatar answered Sep 16 '22 18:09

undetected Selenium