python selenium操作cookie

贲功
2023-12-01
from selenium import webdriver
"""
1. 获取所有的cookie:
    for cookie in driver.get_cookies():
        print(cookie)
2. 根据cookie的key获取value:
    value = driver.get_cookie(key)
3. 删除所有的cookie:
    driver.delete_all_cookies()
4.  删除某个cookie:
    driver.delete_cookie(key)
5. 添加cookie:
    driver.add_cookie({"name":"username","value":"abc"})
"""

driver = webdriver.Chrome(executable_path=r'E:\pyspiderapp\chromedriver87.exe')
driver.get('https://www.baidu.com/')
#1.获取所有cookie
# cookies = driver.get_cookies()
# for cookie in cookies:
#     print(cookie)

#2.根据key来获取cookie
# cookie = driver.get_cookie("expiry")
# print(cookie)

#3.添加cookie
# driver.add_cookie({"name":"username","value":"123"})
#
# # cookies = driver.get_cookies()
# # print(cookies)

#4.根据key删除某个cookie
# driver.add_cookie({"name":"password","value":"11111"})
# cookie1 = driver.get_cookie("password")
# print(cookie1)
# driver.delete_cookie("password")
# print("="*30)
# cookie2 = driver.get_cookie("password")
# print(cookie2)

#5. 删除所有cookie
# cookies = driver.get_cookies()
# print(cookies)
# #删除浏览器里的cookie
# driver.delete_all_cookies()
# print("="*30)
# #查看浏览器里的cookies,再次获得cookies
# print(driver.get_cookies())


 类似资料: