使用selenium在添加cookie提示错误:
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:一个字典对象,带有必需的键 - “name”和“value”;可选键 - ““path”,“domain”,“secure”,“expiry”
发现cookie必须要cookie至少需要name和value两个key。
测试代码:
browser = webdriver.Chrome() browser.get(url) # 删除第一次建立连接时的cookie browser.delete_all_cookies() # cookie = getCookie() # print(type(cookie)) # print(cookie) cookie={"name" : "foo", "value" : "bar"} browser.add_cookie(cookie) browser.get(url)
添加cookie成功
而我之前的cookie没有name和value这两个键,所以添加cookie失败。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。