处理cookie
一、用到的方法
- driver.get_cookies() 获得cookie信息
- add_cookie(cookie_dict) 向cookie添加会话信息
- delete_cookie(name) 删除特定(部分)的cookie
- delete_all_cookies() 删除所有cookie
二、代码举例
1 #coding=utf-8 2 from selenium import webdriver 3 import time 4 5 driver = webdriver.Firefox() 6 driver.get("http://youdao.com") 7 8 #获取cookie并打印 9 cookies = driver.get_cookies() 10 print(cookies) 11 12 #向cookie的name和value添加信息 13 driver.add_cookie({'name':'key-aaaa','value':'value-bbbb'}) 14 cookies = driver.get_cookies() 15 print(cookies) 16 17 #遍历cookies中的name 和value信息打印,当然还有上面添加的信息 18 for cookie in cookies: 19 print("%s -> %s" % (cookie['name'], cookie['value'])) 20 21 #删除一个特定的cookie 22 driver.delete_cookie("key-aaaa") 23 # cookies = driver.get_cookies() 24 # print(cookies) 25 26 #删除所有的cookie 27 driver.delete_all_cookies() 28 time.sleep(2) 29 # cookies = driver.get_cookies() 30 # print(cookies) 31 32 33 34 driver.quit()