1. Get the cookie content of the URL
from selenium import webdriver
import pprint
broswer = webdriver.Chrome()
broswer.get('http://127.0.0.1:8088')
# 输入用户名、密码 并登录
broswer.find_element_by_name('username').send_keys('user')
broswer.find_element_by_name('password').send_keys('123456')
broswer.find_element_by_css_selector('button').click()
# 获取cookie信息,返回的是一个列表
cookies = broswer.get_cookies()
pprint.pprint(cookies)
Two, use cookie to simulate login
Principle: After logging in with the user's password, the cookie is obtained, and the cookie is used to replace the original cookie content when visiting the website to simulate the logged-in state
from selenium import webdriver
import pprint
broswer = webdriver.Chrome()
broswer.get('http://127.0.0.1:8088')
# 以下cookie是登录后拿到的内容
cookies = [{
'domain': '127.0.0.1',
'httpOnly': False,
'name': 'Hm_lpvt_750463144f16fe69eb3ac11bea1c4436',
'path': '/',
'secure': False,
'value': '1613796719'},
{
'domain': '127.0.0.1',
#'expiry': 1645332718, 服务端给的超时时间,需要屏蔽掉
'httpOnly': False,
'name': 'Hm_lvt_750463144f16fe69eb3ac11bea1c4436',
'path': '/',
'secure': False,
'value': '1613796719'},
{
'domain': '127.0.0.1',
#'expiry': 1645332718, 服务端给的超时时间,需要屏蔽掉
'httpOnly': True,
'name': 'beegosessionID',
'path': '/',
'secure': False,
'value': 'dad46322b7619d339690ca13ceecea84'}]
# 先删除原cookie内容
broswer.delete_all_cookies()
# 再逐个添加cookie
for cookie in cookies:
broswer.add_cookie(cookie)
broswer.refresh()
Execution result: You can log in directly without entering the account password