首页 > 代码库 > selenium——cookie操作

selenium——cookie操作

a.获得cookie信息:driver。get_cookies()

 1 #coding=utf-8
 2 from selenium import webdriver
 3 import time
 4 
 5 driver=webdriver.Ie()
 6 driver.get("http://www.youdao.com")
 7 
 8 cookie=driver.get_cookies()   #获取当前页的cookie
 9 print cookie
10 driver.quit()

b.对cookie操作

 1 #coding=utf-8
 2 from selenium import webdriver
 3 import time
 4 
 5 driver=webdriver.Ie()
 6 driver.get("http://www.youdao.com")
 7 
 8 #向cookie中添加信息
 9 driver.add_cookie({name:key-aaaaaaa,value:value-bbbb})
10 
11 for cookie in driver.get_cookies():
12     print "%s -> %s" % (cookie[name],cookie[value])
13 #删除特定cookie
14 driver.delete_cookie("CookieName")
15 #删除所有cookie
16 driver.delete_all_cookies()
17 time.sleep(3)
18 driver.close()

 

selenium——cookie操作