Python + Selenium notes (17th): Operation cookie, pythonselenium
(1)Method
Method |
Simple Description |
Add_cookie (cookie_dict) |
Add cookie information to the current session Cookie_dict: the dictionary, name, and value are required. |
Delete_all_cookies () |
Delete all cookie information in the current session |
Delete_cookie (name) |
Delete cookie information with a single name |
Get_cookie (name) |
Returns the cookie information with a single name. If no information is found, none is returned. |
Get_cookies () |
Returns all cookies of the current session. |
(2)Comparison of cookie before and after Logon
1 from selenium import webdriver 2 import time 3 # driverOption = webdriver. chromeOptions () 4 # driverOption. add_argument (r 'user-data-dir = C: \ Users \ 123456 \ AppData \ Local \ Google \ Chrome \ user data') 5 driver = webdriver. chrome () 6 driver. implicitly_wait (5) 7 driver. maximize_window () 8 driver. get ('https: // www.cnblogs.com/') 9 print ("before Logon:") 10 for ck in driver. get_cookies (): 11 print (ck) 12 login_area = driv Er. find_element_by_css_selector ('# login_area') 13 register = login_area.find_element_by_link_text ('login') 14 register. click () 15 # Wait 25 seconds to manually enter the account password and log on. Remember to check the next Automatic Logon 16 time. sleep (25) 17 print ('post-login ') 18 for ck in driver. get_cookies (): 19 print (ck) 20 driver. quit ()
The output of the comparison shows that after logon, the system adds 'name': '. CNBlogsCookie', 'name': '. Cnblogs. AspNetCore. Cookies '.
(3)Add a cookie to Enable Automatic Logon (you can skip the verification code in this way)
The following code remembers to add the value of value.
1 from selenium import webdriver 2 import time 3 # driverOption = webdriver. chromeOptions () 4 # driverOption. add_argument (r 'user-data-dir = C: \ Users \ 123456 \ AppData \ Local \ Google \ Chrome \ user data') 5 driver = webdriver. chrome () 6 driver. implicitly_wait (5) 7 driver. maximize_window () 8 driver. get ('https: // www.cnblogs.com/') 9 # value. Copy the value printed in step 2.
# Here, the domain name is added because the default domain name is www.cnblogs.com, so the specified domain name 10 # (if the IDE is not easy to copy, you can press ctrl + a to copy it to some document editing software and then copy it back) 11 driver. add_cookie ({12 'domain ':' .cnblogs.com ', 13 'name ':'. CNBlogsCookie ', 14 'value': ''15 16}) 17 # value, just copy the value printed in step 2. 18 driver. add_cookie ({19 'domain ':' .cnblogs.com ', 20 'name ':'. cnblogs. aspNetCore. cookies ', 21 'value': ''22 23} 24) 25 time. sleep (2) 26 # refresh page 27 driver. refresh () 28 time. sleep (3) 29 driver. quit ()
(4)Load Google's user configuration file (this has nothing to do with cookie operations, so I am too lazy to write an article here)
1 from selenium import webdriver 2 import time 3 driverOption = webdriver. chromeOptions () 4 # Set this C: \ Users \ 123456 \ AppData \ Local \ Google \ Chrome \ User Data can be replaced with your own 5 #123456 replace this with the User name of the Local Computer 6 driverOption. add_argument (r 'user-data-dir = C: \ Users \ 123456 \ AppData \ Local \ Google \ Chrome \ user data') 7 driver = webdriver. chrome (chrome_options = driverOption) 8 driver. implicitly_wait (5) 9 driver. maximize_window () 10 driver. get ('https: // www.cnblogs.com/') 11 time. sleep (3) 12 driver. quit ()