1. 程式人生 > >selenium讀取、新增、刪除cookie

selenium讀取、新增、刪除cookie

讀取cookie

讀取所有cookie:get_cookies()

讀取指定cookie:get_cookie(name)

新增cookie:add_cookie(dict)

刪除cookie

刪除所有cookie:delete_all_cookies()

刪除指定cookie:delete_cookie(name)

 #coding=utf-8
from selenium import webdriver

dr = webdriver.Firefox()
dr.get("https://passport.bilibili.com/login")

dr.add_cookie({ u
'name': u'zhangsan', u'value': u'zhangsan1234'}) #新增張三的cookie #按指定格式列印 for cookie in dr.get_cookies(): print "%s/%s" % (cookie['name'],cookie['value']) dr.delete_cookie( u'zhangsan') #指定刪除張三的cookie print '--after delete cookies--' for cookie in dr.get_cookies(): print "%s/%s" % (cookie['
name'],cookie['value']) dr.quit()
#返回結果
sid/hw6452jw finger/964b42c0 buvid3/1DB418E1-9AE4-49BA-8E1A-3C099020275D13519infoc zhangsan/zhangsan1234 #新增的張三cookie --after delete cookies-- sid/hw6452jw finger/964b42c0 buvid3/1DB418E1-9AE4-49BA-8E1A-3C099020275D13519infoc #已經刪除了張三的cookie