6_5 selenium操作cookie

 

 1 """selenium操作cookie"""
 2 
 3 
 4 from selenium import webdriver
 5 
 6 driver_path = r"D:\install\chromedriver\chromedriver.exe"
 7 driver = webdriver.Chrome(executable_path = driver_path)
 8 driver.get('https://www.baidu.com/')
 9 
10 # 获取当前浏览器所有cookie信息
11 for cookie in driver.get_cookies():
12     print(cookie)
13 
14 print('*'*40)
15 # 根据cookie的key值获取value
16 print(driver.get_cookie('PSTM'))
17 
18 print('*'*40)
19 # 删除某个cookie
20 driver.delete_cookie("PSTM")
21 print(driver.get_cookie('PSTM'))
22 # 删除所有 cookie
23 driver.delete_all_cookies()
24 
25 # 添加cookie信息
26 driver.add_cookie({'name': 'name', 'domain': 'www.baidu.com', 'value': 'germey'})
27 print(driver.get_cookies())

 

你可能感兴趣的:(6_5 selenium操作cookie)