from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("http://www.youdao.com")

#获得cookie信息
cookie = driver.get_cookies()

print (cookie)
#获得的cookie
'''
[{'name': 'YOUDAO_MOBILE_ACCESS_TYPE', 'value': '1', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False, 'expiry': 1580193376},
{'name': 'DICT_UGC', 'value': 'be3af0da19b5c5e6aa4e17bd8d90b28a|', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False},
{'name': 'OUTFOX_SEARCH_USER_ID', 'value': '-841552186@117.30.149.1', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False, 'expiry': 2494737376},
{'name': 'JSESSIONID', 'value': 'abc6BZU1MfXNueY0dltIw', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False},
{'name': '___rl__test__cookies', 'value': '1548657380561', 'path': '/', 'domain': 'www.youdao.com', 'secure': False, 'httpOnly': False},
{'name': 'OUTFOX_SEARCH_USER_ID_NCOO', 'value': '1613640556.2428505', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False, 'expiry': 1611729380}]
'''
#driver.add_cookie({'name':'key-aaaaa','value':'v1111'})
#遍历cookies中的namet value的信息打印
for cookie in driver.get_cookies():
print ('%s -> %s'%(cookie['name'],cookie['value']))

#通过cookie的name查找内容
nn=driver.get_cookie("OUTFOX_SEARCH_USER_ID'")
print (nn)

driver.delete_cookie("OUTFOX_SEARCH_USER_ID")
second=driver.get_cookie("OUTFOX_SEARCH_USER_ID")
print("second %s"%second)
driver.quit()

 

运行结果:

[{'name': 'YOUDAO_MOBILE_ACCESS_TYPE', 'value': '1', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False, 'expiry': 1580195742}, {'name': 'DICT_UGC', 'value': 'be3af0da19b5c5e6aa4e17bd8d90b28a|', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False}, {'name': 'OUTFOX_SEARCH_USER_ID', 'value': '1381790709@117.30.149.1', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False, 'expiry': 2494739742}, {'name': 'JSESSIONID', 'value': 'abclBSe0I0-3i9zAfutIw', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False}, {'name': '___rl__test__cookies', 'value': '1548659748355', 'path': '/', 'domain': 'www.youdao.com', 'secure': False, 'httpOnly': False}, {'name': 'OUTFOX_SEARCH_USER_ID_NCOO', 'value': '1491590976.7487862', 'path': '/', 'domain': '.youdao.com', 'secure': False, 'httpOnly': False, 'expiry': 1611731748}]
YOUDAO_MOBILE_ACCESS_TYPE -> 1
DICT_UGC -> be3af0da19b5c5e6aa4e17bd8d90b28a|
OUTFOX_SEARCH_USER_ID -> 1381790709@117.30.149.1
JSESSIONID -> abclBSe0I0-3i9zAfutIw
___rl__test__cookies -> 1548659748355
OUTFOX_SEARCH_USER_ID_NCOO -> 1491590976.7487862
None
second None
[Finished in 62.7s]