How to fix quot;invalid argument: invalid #39;expiry#39;quot; in Selenium when adding cookies to a chromedriver?(如何修复“无效的参数:无效的‘到期’在向 chromedriver 添加 cookie 时在 Selenium 中?)
问题描述
我正在尝试将 cookie 添加到浏览器,但出现以下错误:
I'm trying to add cookies to a browser, but getting the following error:
消息:无效参数:无效的到期"(会话信息:chrome=75.0.3770.90)
Message: invalid argument: invalid 'expiry' (Session info: chrome=75.0.3770.90)
此代码显示了我如何加载和添加 cookie.
This code shows how I load and add cookies.
for cookie in pickle.load(open(r'{0}{1}_cookie.pkl'.format(settings.COOKIES_PATH, self.tv_username), 'rb')):
self.browser.add_cookie(cookie)
cookie 的值:
{'__utmc': '226258911', '_sp_id.cf1a': '0b243b32-8dee-46d9-a243-bb4d2bfcb805.1560942815.1.1560942821.1560942815.5941cbc0-0500-4a17-8f6c-4ee3f133f67c', 'km_vs': '1', '__utmt': '1', '__utma': '226258911.510671571.1560942814.1560942814.1560942814.1', 'km_ai': 'm6caeIAUtEqx%2BIWda%2F7klZER%2F1Y%3D', 'kvcd': '1560942821112', '__utmb': '226258911.2.10.1560942814', 'km_lv': '1560942821', '__utmz': '226258911.1560942814.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)', 'etg': 'undefined', '_sp_ses.cf1a':'*'}
它适用于以前版本的 chrome.任何帮助将不胜感激!
It worked fine with previous version of chrome. Any help would be appreciated!
推荐答案
问题是您尝试添加的 cookie 格式与 selenium 预期的格式不同.
The problem is that you are trying to add the cookies with a different format than the selenium expects.
python selenium api 参考 说你必须用这样的字典插入 cookie
The python selenium api reference says that you have to insert the cookies with a dict like that
driver.add_cookie({'name' : 'foo', 'value' : 'bar'})
因此您必须调整循环以使用键值格式
So you have to adapt your loop to use a key,value format
for key, value in pickle.load(open(r'{0}{1}_cookie.pkl'.format(settings.COOKIES_PATH, self.tv_username), 'rb')):
self.browser.add_cookie({'name' : key, 'value' : value})
这篇关于如何修复“无效的参数:无效的‘到期’"在向 chromedriver 添加 cookie 时在 Selenium 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何修复“无效的参数:无效的‘到期’"在向 chromedriver 添加 cookie 时在 Selenium 中?
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01