Implementation of python selenium operation cookie

Since the HTTP protocol is a stateless protocol, when the server communicates with the client through the http protocol, the server cannot record the client's information.

In order to solve this problem, it is realized through Cookie and Session technology.

Cookie attributes

The attributes of general cookies include:

Domain: Domain, which indicates which domain or subdomain the current cookie belongs to.

For the Set-Cookie returned by the server, if the Domain value is not specified, then the Domain value defaults to the main domain name corresponding to the currently submitted http request. For example, when visiting http://www.example.com and returning a cookie without a named domain value, it will be the default www.example.com.

Path: Indicates the path of the cookie.

Expire time/Max-age: indicates the validity period of the cookie. The value of expire is a time, after this time, the cookie becomes invalid. Or use max-age to specify the time after which the current cookie expires. If a cookie returned by the server does not specify its expire time, it means that the validity period of the cookie is only the current session, that is, the session cookie. After the current session ends, it expires. Correspondingly, when the page is closed (in the browser), the cookie should be deleted by the browser.

secure: indicates that the cookie can only be transmitted using https. Generally used for cookies containing authentication information. When requesting to transmit this cookie, it must be transmitted using https.

httponly: Indicates that this cookie must be used for http or https transmission. This means that browser scripts, such as javascript, are not allowed to access and manipulate this cookie.

Get cookie

# Log in to your Baidu account from the Baidu homepage and get cookies
from selenium import webdriver
from time import sleep
import json
 
# Launch chome browser and visit Baidu homepage
option=webdriver.ChromeOptions()
option.add_argument('--start-maximized')
drive=webdriver.Chrome(options=option)
drive.get('http://www.baidu.com')print(drive.title)
 
# Click to Login"
eled=drive.find_element_by_link_text("log in")
eled.click()sleep(2)
 
# Click "Username Login"
namelogin=drive.find_element_by_css_selector('p.tang-pass-footerBarULogin')
namelogin.click()sleep(2)
 
# Enter username, password and log in
username = drive.find_element_by_id('TANGRAM__PSP_10__userName')
username.send_keys('********')sleep(1)
 
password = drive.find_element_by_id('TANGRAM__PSP_10__password')
password.send_keys('********')sleep(1)
 
submit = drive.find_element_by_id('TANGRAM__PSP_10__submit')
submit.click()sleep(3)
 
# Get website cookies
diccookie=drive.get_cookies()
fw=open('baiducookie.txt','w')
json.dump(diccookie,fw)
fw.close()

Read the cookie from the hard disk and add it to the request sending server.

import time
import json
 
Launch chome browser and visit Baidu homepage
option=webdriver.ChromeOptions()
option.add_argument('--start-maximized')
drive=webdriver.Chrome(options=option)
 
drive.get('http://www.baidu.com')print(drive.title)
 
fr=open('baidu_cookie.txt','r')
cookielist=json.load(fr)
fr.close()for cookie in cookielist:
 drive.add_cookie(cookie)sleep(5)
drive.get('http://www.baidu.com')
drive.refresh()

Failed to log in to Baidu through cookie:

Message: unable to set cookie
Message: invalid argument: invalid ‘expiry’

So far, this article on the implementation of python selenium operation cookie is introduced. For more related python selenium operation cookie content, please search for the previous article of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future. !

Recommended Posts

Implementation of python selenium operation cookie
Python preliminary implementation of word2vec operation
Python implementation of gomoku program
Detailed implementation of Python plug-in mechanism
Implementation of reverse traversal of python list
Python implementation of IOU calculation case
Implementation of python3 registration global hotkey
Implementation of python student management system
Some examples of python operation redis
Example operation of python access Alipay
Implementation of python gradient descent algorithm
The operation of python access hdfs
Basic analysis of Python turtle library implementation
Implementation of JWT user authentication in python
Python implementation of intersection and IOU tutorial
Implementation principle of dynamic binding of Python classes
7 features of Python3.9
Python file operation
python operation kafka
Analysis of common methods of Python operation Jira library
Implementation of Python headless crawler to download files
Python implementation of AI automatic matting example analysis
Python implementation of hand drawing effect example sharing
Implementation of business card management system with python
Example of feature extraction operation implemented in Python
Python writes the game implementation of fishing master
Implementation of business card management system based on python
Detailed explanation of the implementation steps of Python interface development
Basics of Python syntax
Python operation SQLite database
Basic syntax of Python
Basic knowledge of Python (1)
Prettytable module of python
Python operation yaml instructions
09. Common modules of Python3
Learn the hard core operation of Python in one minute
Python automated operation and maintenance 2
Consolidate the foundation of Python (4)
Python operation Excel merge cells
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Python analysis of wav files
Consolidate the foundation of Python(6)
Python drawing rose implementation code
Python negative modulus operation example
Analysis of JS of Python crawler
python king of glory wallpaper
Consolidate the foundation of Python(5)
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Deep understanding of Python multithreading
Python automated operation and maintenance 1
Analysis of Python object-oriented programming
Implementation of CentOS8.0 Network Configuration
Python version of OpenCV installation
Quick start Python file operation
Python GUI simulation implementation calculator
Selenium visual crawler for python crawler
9 feature engineering techniques of Python
matplotlib of python drawing module