selenium.common.exceptions.WebDriverException: Message: invalid argument: missing 'name'

created at 07-17-2021 views: 1

Use selenium to add a cookie prompt error:

Traceback (most recent call last):
  File "I:/Dropbox/python/spider/zhihu/post.py", line 37, in <module>
    z.loadCookie()
  File "I:/Dropbox/python/spider/zhihu/post.py", line 31, in loadCookie
    self.browser.add_cookie(cookie)
  File "D:\software\python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 760, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
  File "D:\software\python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "D:\software\python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: missing 'name'
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.10586 x86_64)
  • cookie_dict: a dictionary object with required keys-"name" and "value"; optional keys-"path", "domain", "secure", "expiry"

It is found that the cookie must require at least two keys: name and value.

browser = webdriver.Chrome()
browser.get(url)
# Delete the cookie when the connection is first established
browser.delete_all_cookies()
# cookie = getCookie()
# print(type(cookie))
# print(cookie)
cookie={"name": "foo", "value": "bar"}
browser.add_cookie(cookie)
browser.get(url)

Added cookie successfully

My previous cookie did not have the two keys of name and value, so adding the cookie failed.

Please log in to leave a comment.