不乱于心,不困于情。
不畏将来,不念过往。如此,安好。

python 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
赞(0)
未经允许不得转载:seo优化_前端开发_渗透技术 » python selenium读取、添加、删除cookie