I\'m trying to push cookies to selenium firefox webdriver stored from previous session, but I got error:
org.openqa.selenium.InvalidCookieDomainException: Do
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.
As per the HTML-Living Standard Specification a Document Object
may be categorized as a cookie-averse Document object in the following circumstances :
Browsing Context
.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
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()
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')
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.