selenium - cookie operations

Foreplay

Doing the automation of time, he met intractable verification code, we can manually log in, and then log on to get the cookie, added to the browser, you can log on to achieve

Real

from Selenium Import the webdriver 

Driver = webdriver.Chrome () 
driver.get ( ' http://www.imooc.com ' ) 
Cookie = driver.get_cookies ()   # give Cookie 
Print (Cookie)   # print out a list, the list in a dictionary 
Print (cookie [0])   # print a list of the first one 
driver.add_cookie ({ ' name ' : ' zouzou ' , ' value ' : ' 123 ' }) 

# to add a cookie in the dictionary, is added to the last one 
forCookie in driver.get_cookies ():
     Print ( " % S% S ----- " % (Cookie [ ' name ' ], Cookie [ ' value ' ]))
     # loop to print out the name and value 
driver.quit ()

Use bypass the login cookie

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.add_cookie({'name': 'BAIDUID', 'value': 'ASKMLJIH'})
driver.add_cookie({'name': 'BDUSS', 'value': 'UHUIHIU'})
sleep(3)
driver.refresh()
Several methods cookie operation 
1.get_cookies (): Get all Cookies
2.driver.get_cookie (name): Get the name specified by cookie:
3. Clears the cookie: delete_cookie ()
4.delete_all_cookies (): clear all Cookies
. 5. add_cookie (cookie_dict): added value of the cookie

Guess you like

Origin www.cnblogs.com/zouzou-busy/p/11219901.html