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?
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.
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.
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 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')
You can find a detailed discussion in:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With