selenium.common.exceptions.WebDriverException: Message: unable to set cookie

Created on 2 Feb 2018  路  5Comments  路  Source: SeleniumHQ/selenium

Hi,all. I'm using the selenium occur some problem as blow. python version is Python 3.5.3
the always exception error.

save cookies

pickle.dump( browser.get_cookies() , open("cookies.ini","wb"))

restore cookies

 cookies = pickle.load(open("cookies.ini", "rb"))
    for cookie in cookies:
        print("cookies type ",type(cookie))
        print("dict ",cookie)
        browser.add_cookie(cookie)

* Exception *

 File "mooc-scrapy.py", line 211, in <module>
    browser.add_cookie(cookie)
  File "/home/lcy/.pyenv/versions/py3dev/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 775, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
  File "/home/lcy/.pyenv/versions/py3dev/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
    self.error_handler.check_response(response)
  File "/home/lcy/.pyenv/versions/py3dev/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unable to set cookie
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.34.522913 (36222509aa6e819815938cbf2709b4849735537c),platform=Linux 4.9.0-5-amd64 x86_64)

Most helpful comment

@lmtierney After running the test suite many, many, many times, this all appears to be a timing issue. Suddenly, I was able to make the tests fail for Python 2.5, and work fine under Python 3.5, but in the end, all the failures were intermittent.

Then I realized that even setting or clearing cookies is non-blocking! I could delete all cookies from the driver and set new ones, and attempt to verify the change immediately after, but the original cookie list appeared to remain unchanged.

The issue appears to have been resolved for us by:

  1. Ensuring we wait for the first page to load fully (to have a domain context for the cookie)
  2. Setting the cookie.
  3. Using the WebDriverWait mechanism to _wait_ for the cookie to actually become present in driver.get_cookies()

Leaving this comment here in the hopes that it may be useful for someone else.

All 5 comments

Please report this issue to chromedriver project: https://sites.google.com/a/chromium.org/chromedriver/help

Hi @barancev, we're having the same problem, and I'm quite unsure how it relates to chromedriver itself.

In fact, this works just fine when testing under Python 2.7.13, but the same code fails under Python 3.5.3.

We have

Selenium==3.4.3
Chromedriver==2.35.528139
Chrome==64.0.3282.186

Since the only difference here is the Python version, I don't understand how chromedriver is to blame...

@lunkwill42 my guess would be that pickle is behaving differently between the two python versions. Selenium does nothing different between versions as far as cookies go

@lmtierney After running the test suite many, many, many times, this all appears to be a timing issue. Suddenly, I was able to make the tests fail for Python 2.5, and work fine under Python 3.5, but in the end, all the failures were intermittent.

Then I realized that even setting or clearing cookies is non-blocking! I could delete all cookies from the driver and set new ones, and attempt to verify the change immediately after, but the original cookie list appeared to remain unchanged.

The issue appears to have been resolved for us by:

  1. Ensuring we wait for the first page to load fully (to have a domain context for the cookie)
  2. Setting the cookie.
  3. Using the WebDriverWait mechanism to _wait_ for the cookie to actually become present in driver.get_cookies()

Leaving this comment here in the hopes that it may be useful for someone else.

I solved same problem (i craft 'cookies.pkl' previously):

  1. Firstly get the page of site: driver.get('domain_name_of_site')
  2. Secondly, add cookies to driver: for cookie in pickle.load(open("cookies.pkl", "rb")): driver.add_cookie(cookie)
Was this page helpful?
0 / 5 - 0 ratings