Selenium入门13 cookie的增删改查

cookie的增删改查:

查询:get_cookies()查询所有cookie,get_cookie(cookie的name)获取单个cookie

删除:delete_cookie(cookie的name)

增加和修改:add_cookie({"name":"cookie name",value:"cookie value"})

 

#coding=utf-8
#cookie的增删改查

from selenium import webdriver

dr=webdriver.Firefox()
dr.get("http://www.baidu.com")

#所有cookies查询
cookies = dr.get_cookies()
print('cookies的类型: ',type(cookies)) #打印出来是list类型
print('cookie的类型: ',type(cookies[0])) #元素是dict类型
print('打印出所有的cookie: ')
for cookie in cookies: 
    #print(cookie)
    print(cookie["name"],cookie["value"])

#查询单个cookie
print("baiduid:", dr.get_cookie('BAIDUID'))
#删除cookie
dr.delete_cookie("BAIDUID")
print(dr.get_cookie("BAIDUID")) #已删除 none
#增加cookie
dr.add_cookie({"name":"testcookie","value":"testcookievalue"})
print(dr.get_cookie('testcookie'))
#修改cookie
dr.add_cookie({"name":"testcookie","value":"modify-testcookievalue"})
print(dr.get_cookie("testcookie"))

dr.quit()

 

posted @ 2018-10-03 19:11  dinghanhua  阅读(631)  评论(0编辑  收藏  举报