This section focuses on:
Driver.get_cookies () Get cookie information
Add_cookie (COOKIE_DICT) Adds session information to cookies
Delete_cookie (name) deletes a specific (partial) cookie
Delete_all_cookies () Delete all Cookies
It's fun to manipulate cookies through webdriver, and sometimes we need to know if a cookie is in the browser, Webdriver can help us read, add, and delete cookies.
Print cookie Information
#coding =utf-8 from
Selenium import webdriver
import time
driver = Webdriver. Chrome ()
driver.get ("http://www.youdao.com")
# Get cookie information
cookie= driver.get_cookies ()
# Prints the print
cookie
driver.quit () with the information for the cookie.
To run Print information:
[{u ' domain ': U '. youdao.com ', U ' secure ': False, U ' value ': U ' agfzbg9nz2vkpxrydwu= ', u ' expiry ': 1408430390.991375, U ' path ': U '/', U ' name ': U ' _pref_anonyuser__myth '}, {u ' domain ': U '. youdao.com ', U ' secure ': False, U ' value ': U ' 1777851312@ 218.17.158.115 ', u ' expiry ': 2322974390.991376, U ' path ': U '/', U ' name ': U ' outfox_search_user_id '}, {u ' path ': U '/', U ' domain ': U ' www.youdao.com ', U ' name ': U ' jsessionid ', U ' value ': U ' abcux9zdw0minadihtvcu ', U ' secure ': False}]
Actions on Cookies
The way it printed all the cookie information table, too much too messy, we just want to have the right sex to print the information we want to see the following example
#coding =utf-8 from
Selenium import webdriver
import time
driver = Webdriver. Firefox ()
driver.get ("http://www.youdao.com")
adds session information #向cookie的name and value.
Driver.add_cookie ({' name ': ' key-aaaaaaa ', ' value ': ' value-bbbb '})
#遍历cookies中的name and value information is printed and, of course, the information added above
for Cookies in Driver.get_cookies ():
print '%s->%s '% (cookie[' name ', cookie[' value '])
# There are two ways to delete cookies
# Delete a specific cookie
Driver.delete_cookie ("CookieName")
# Delete all Cookies
Driver.delete_all_cookies ()
time.sleep (2)
driver.close ()
To run Print information:
Youdao_mobile_access_type-> 1
_pref_anonyuser__myth-> agfzbg9nz2vkpxrydwu=
OUTFOX_SEARCH_USER_ID -> -1046383847@218.17.158.115
jsessionid-> abc7qse_sbgsvgnvlbvcu key-aaaaaaa-> value-bbbb # This one was added by ourselves
Analyzing cookies by logging in to a blog park
#coding =utf-8 from
Selenium import webdriver
import time
driver = Webdriver. Firefox ()
driver.get ("Http://passport.cnblogs.com/login.aspx?") Returnurl=http://www.cnblogs.com/fnng/admin/editposts.aspx ")
Time.sleep (3)
Driver.maximize_window () # The browser displays full screen
#通过用户名密码登陆
driver.find_element_by_id ("Tbusername"). Send_keys ("Fnngj")
Driver.find_element_ by_id ("Tbpassword"). Send_keys ("123456")
#勾选保存密码
driver.find_element_by_id ("Chkremember"). Click ()
Time.sleep (3)
#点击登陆按钮
driver.find_element_by_id ("Btnlogin"). Click ()
#获取cookie信息并打印
Cookie= driver.get_cookies ()
print Cookie
time.sleep (2)
driver.close ()
To run Print information:
#第一次执行信息
>>>
[{u ' domain ': U '. cnblogs.com ', U ' name ': U '. Dottextcookie ', U ' value ': U ' C709F15A8BC0B3E8D9AD1F68B371053849F7FEE31F73F1292A150932FF09A7B0D4A1B449A32A6B24AD986CDB05B9998471A37F39C3B637E85E481AA 986d3f8c187d7708028f9d4ed3b326b46dc43b416c47b84d706099ed1d78b6a0fc72dcf948db9d5cbf99d7848fdb78324 ', u ' expiry ': None, U ' path ': U '/', U ' secure ': False}]
>>> ========================= Restart ========================== = = = = = =
#第二次执行信息
>>>
[{u ' domain ': U '. cnblogs.com ', U ' name ': U '. Dottextcookie ', U ' value ': U ' 5bb735cad62e99f8ccb9331c32724e2975a0150d199f4243ad19357b3f99a416a93b2e803f4d5c9d065429713be8b5db4ed760edcbaf492eabe2158 B3a6fbbea2b95c4da3d2efeadacc3247040906f1462731f652199e2a8befd8a9b6aae87cf3059a3caeb9ab0d8b1b7ad2a ', u ' expiry ': 1379502502, U ' path ': U '/', U ' secure ': False}]
>>>
First comment out the check to save the password operation, the second by checking the save password to get cookie information; see how the Cookies for the two run results are different:
U ' expiry ': None
U ' expiry ': 1379502502
By comparison, it is found that the expiry value is none when the password is saved. Then you can initially determine that the action to save the password is useful in cookies. As to whether the accuracy can be further analysis.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/