Webdriver provides a way to manipulate cookies to read, add, and delete cookie information.
Webdriver How to manipulate cookies:
Get_cookies (): Get all cookie information.
Get_cookie (name): Returns the cookie information for the dictionary key "name".
Add_cookie (cookie_dict): Add a cookie. "Cookie_dict" refers to the Dictionary object, which must have the name and value values.
Delete_cookie (name,optionsstring): Delete cookie information. "Name" is the name of the cookie to be deleted, and "optionsstring" is the option for the cookie, and the options currently supported include "path", "Domain".
Delete_all_cookies (): Delete all cookie information.
The following is a get_cookies () to obtain cookie information for the current browser.
From selenium import webdriverdriver = Webdriver. Chrome () driver.get ("http://www.youdao.com") # get cookie Information cookie = driver.get_cookies () print (cookie) Driver.quit ()
Execution Result:
[{' domain ': ' www.youdao.com ', ' httponly ': false, ' name ': ' ___rl__test__cookies ', ' path ': '/', ' secure ': false, ' value ': ' 1526219954928 '}, {' domain ': '. youdao.com ', ' expiry ': 1589291954, ' httponly ': False, ' name ': ' Outfox_search_user_id_ Ncoo ', ' path ': '/', ' secure ': False, ' value ': ' 900350754.7866102 '}] |
As can be seen from the execution result, cookie data is stored in the form of a dictionary.
To add a cookie action:
From selenium import webdriverdriver = Webdriver. Chrome () driver.get ("http://www.youdao.com") # adds new information to the cookie Driver.add_cookie ({' name ': ' key-aaaaa ', ' value ': ' VALUE-BBBBB '}) # traversal cookie information for cookie in Driver.get_cookies (): print ("%s"% (cookie[' name '), cookie[' value ' ])) Driver.quit ()
Execution Result:
outfox_search_user_id, [email protected] DICT_UGC-be3af0da19b5c5e6aa4e17bd8d90b28a| Jsessionid-ABCL3HQPWO42I51W90ZNW ___rl__test__cookies-1526220445878 KEY-AAAAA-VALUE-BBBBB Outfox_search_user_id_ncoo-828629148.5563225 |
Article excerpt from: http://www.testclass.net/selenium_python/cookie/
Selenium-webdriver-cookie Action