org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse using Selenium and WebDriver
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
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 webdriverimport pickledriver = 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 cookiespickle.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 webdriverimport pickledriver = webdriver.Chrome()driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')# loading the stored cookiescookies = 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:
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.