python selenium库添加cookie,登录时还是没有登录上,如何解决?
在网上搜了好多博客,根据博客写了两个函数,分别获取cookie和添加cookie并测试登录.def get_cookie():
dirver = webdriver.Firefox()
dirver.get('https://juejin.cn/')
dictCookies = dirver.get_cookies()# 获得所有cookie信息(返回是字典)
print(dictCookies)
print(type(dictCookies))
jsonCookies = json.dumps(dictCookies)# dumps是将dict转化成str格式
print(jsonCookies)
print(type(jsonCookies))
# 登录完成后,将cookies保存到本地文件
time.sleep(60)
with open("cookies_fofa.json", "w") as fp:
fp.write(jsonCookies)
print(jsonCookies)
print(type(jsonCookies))
def ceshi():
browser = webdriver.Firefox()
browser.get("https://juejin.cn/")
browser.delete_all_cookies()# 删除所有cookie信息
with open('cookies_fofa.json', 'r', encoding='utf-8') as f:
listCookies = json.loads(f.read())# loads是将str转化成dict格式
print(listCookies)
for cookie in listCookies:
browser.add_cookie(cookie)
# print(cookie.get("name")+cookie.get("value"))
#browser.add_cookie({# 此处模仿之前生成的cookies_fofa.json的格式写即可,要用单引号,因为已经转成dict格式了
# 'name': cookie.get("name"),
# 'value': cookie.get("value"),
# 'path':cookie.get("path"),
# # 'domain':cookie.get("domain"),
# 'secure': False,
# 'httpOnly': False,
# 'expiry':cookie.get("expiry"),
# 'sameSite':cookie.get("sameSite")
#})
browser.get("https://juejin.cn/")
get_cookie()
ceshi()
``
程序输出:几乎一模一样,除了false的f的大小写.
```python
[{'name': 'ttcid', 'value': 'd473b09648ae452d8a04fa111af9bc4c38', 'path': '/', 'domain': 'juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1683563767, 'sameSite': 'None'}, {'name': '_tea_utm_cache_2608', 'value': 'undefined', 'path': '/', 'domain': '.juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1746635767, 'sameSite': 'None'}, {'name': '_ga', 'value': 'GA1.2.2070251714.1652027768', 'path': '/', 'domain': '.juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1715099767, 'sameSite': 'None'}, {'name': '_gid', 'value': 'GA1.2.1692958009.1652027768', 'path': '/', 'domain': '.juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1652114167, 'sameSite': 'None'}, {'name': 'MONITOR_WEB_ID', 'value': '5b2cf523-605f-4560-83f1-ec4aaa802dc8', 'path': '/', 'domain': '.juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1659803767, 'sameSite': 'None'}]
<class 'list'>
[{"name": "ttcid", "value": "d473b09648ae452d8a04fa111af9bc4c38", "path": "/", "domain": "juejin.cn", "secure": false, "httpOnly": false, "expiry": 1683563767, "sameSite": "None"}, {"name": "_tea_utm_cache_2608", "value": "undefined", "path": "/", "domain": ".juejin.cn", "secure": false, "httpOnly": false, "expiry": 1746635767, "sameSite": "None"}, {"name": "_ga", "value": "GA1.2.2070251714.1652027768", "path": "/", "domain": ".juejin.cn", "secure": false, "httpOnly": false, "expiry": 1715099767, "sameSite": "None"}, {"name": "_gid", "value": "GA1.2.1692958009.1652027768", "path": "/", "domain": ".juejin.cn", "secure": false, "httpOnly": false, "expiry": 1652114167, "sameSite": "None"}, {"name": "MONITOR_WEB_ID", "value": "5b2cf523-605f-4560-83f1-ec4aaa802dc8", "path": "/", "domain": ".juejin.cn", "secure": false, "httpOnly": false, "expiry": 1659803767, "sameSite": "None"}]
<class 'str'>
[{"name": "ttcid", "value": "d473b09648ae452d8a04fa111af9bc4c38", "path": "/", "domain": "juejin.cn", "secure": false, "httpOnly": false, "expiry": 1683563767, "sameSite": "None"}, {"name": "_tea_utm_cache_2608", "value": "undefined", "path": "/", "domain": ".juejin.cn", "secure": false, "httpOnly": false, "expiry": 1746635767, "sameSite": "None"}, {"name": "_ga", "value": "GA1.2.2070251714.1652027768", "path": "/", "domain": ".juejin.cn", "secure": false, "httpOnly": false, "expiry": 1715099767, "sameSite": "None"}, {"name": "_gid", "value": "GA1.2.1692958009.1652027768", "path": "/", "domain": ".juejin.cn", "secure": false, "httpOnly": false, "expiry": 1652114167, "sameSite": "None"}, {"name": "MONITOR_WEB_ID", "value": "5b2cf523-605f-4560-83f1-ec4aaa802dc8", "path": "/", "domain": ".juejin.cn", "secure": false, "httpOnly": false, "expiry": 1659803767, "sameSite": "None"}]
<class 'str'>
[{'name': 'ttcid', 'value': 'd473b09648ae452d8a04fa111af9bc4c38', 'path': '/', 'domain': 'juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1683563767, 'sameSite': 'None'}, {'name': '_tea_utm_cache_2608', 'value': 'undefined', 'path': '/', 'domain': '.juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1746635767, 'sameSite': 'None'}, {'name': '_ga', 'value': 'GA1.2.2070251714.1652027768', 'path': '/', 'domain': '.juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1715099767, 'sameSite': 'None'}, {'name': '_gid', 'value': 'GA1.2.1692958009.1652027768', 'path': '/', 'domain': '.juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1652114167, 'sameSite': 'None'}, {'name': 'MONITOR_WEB_ID', 'value': '5b2cf523-605f-4560-83f1-ec4aaa802dc8', 'path': '/', 'domain': '.juejin.cn', 'secure': False, 'httpOnly': False, 'expiry': 1659803767, 'sameSite': 'None'}]请问我的问题出在了那里?
在加入新的cookie后,需要调用driver.refresh()函数刷新driver。 看下日志把 看下日志 刷新一下呢? 查看方法对不对
页:
[1]