WebDriver provides related methods of operation Cookie, you can read, add, and delete cookie information, the following will explain respectively
模拟登陆并获取Cookies
,添加Cookies自动登录
two small examples.
table of Contents
The key method used in this chapter as follows:
-
get_cookies()
: Get all the cookie information. -
get_cookie(name)
: Returns a dictionary key for the "name" of the cookie information. -
add_cookie(cookie_dict)
: Add cookie. "Cookie_dict" refers to a dictionary object, you must have the name and value value. -
delete_cookie(name,optionsString)
: Delete cookie information. "Name" is the name of the cookie you want to delete, "optionsString" is the cookie option, currently supported options include the "path", "field." -
delete_all_cookies()
: Delete all cookie information.
1, simulated landing and get Cookies
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
# 模拟登陆
driver.find_element_by_link_text('登录').click()
time.sleep(2)
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__footerULoginBtn"]').click()
time.sleep(2)
driver.find_element_by_name("userName").send_keys("账号")
driver.find_element_by_name("password").send_keys("密码")
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__submit"]').click()
time.sleep(20) #可能会出现验证码,手动点一下
# 获取cookies
cookies = driver.get_cookies()
print(cookies)
driver.quit()
2, add the automatic logon Cookies
Note:
- Cookies get when each dictionary field is not uniform, add all will complain, so I just added a few more important field.
- The list code field into cookies on step acquisition of content, or build your own.
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
time.sleep(2)
cookies = [] #换成上一步获取的Cookies
for cookie in cookies:
driver.add_cookie(
{
'domain':cookie['domain'],
'name': cookie['name'],
'value':cookie['value'],
'path': cookie['path']
}
)
# # 刷新页面
driver.refresh()
# driver.quit() #为方便查看,页面就不关了
Selenium anthology Portal:
title | Brief introduction |
---|---|
Python crawler - Selenium (1) easy to install and use | Details of installation and simple to use Selenium is dependent on the environment of Windows and Centos7 |
Python crawler - Selenium (2) and positioning elements common methods WebDriver | Details of the positioning element 8 ways and cooperate click and enter, submit, using the method of obtaining information assertion |
Python crawler - Selenium (3) common method of controlling the browser | Details of using a custom browser window or full-screen size, browser control back, forward, refresh your browser and other methods of |
Python reptile - Selenium (4) configuration parameters startup items | Details of the configuration parameters Selenium startup items including no interface mode, the browser window size, the browser User-Agent (request header), etc. |
Python reptile - Selenium (5) mouse events | Details of use of right-click, double click, drag, hover, etc. |
Python reptile - Selenium (6) key events | Details of operation of the keyboard, includes almost all common keycaps and key combinations |
Python crawler - Selenium (7) multi-window switch | Selenium is described in detail how to implement the freedom to switch between different windows |
Python crawler - Selenium (8) frame / iframe nested form page | Details of how to switch from the current positioning of the body frame / iframe embedded in a page form |
Python crawler - Selenium (9) alert box (pop) Processing | Details of how to locate and deal with many types of warning popups |
Python crawler - Selenium (10) treated drop-down box | Details on how to locate and deal with flexible drop-down box |
Python reptile - Selenium (11) file upload | Details of how elegant by send_keys () the specified file upload |
Python reptile - Selenium (12) for login Cookies, Cookies automatically log in and add | Details of how to obtain and use Cookies Cookies for automatic logon |
Python crawler - Selenium (13) element disposed wait | Details how elegant set of elements waiting time, to prevent the program from running too fast positioning element failure |
Python crawler - Selenium (14) screen shot | Details on how to use the screen shot |
Python reptile - Selenium (15) closes the browser | Close the window detailed describes two differences |
Welcome Message Tucao