[selenium] use cookies

Methods for WebDriver to interact with cookies.

cookie

It is usually used by websites to identify users, maintain login status or track user browsing records.

  • Name: the name of the cookie
  • Value: the value of the cookie
  • Domain: hosts that are allowed to receive cookies
  • Path: request URL path
  • Expires/Max-Age: cookie expiration date
  • Size: the size of the cookie
  • HttpOnly: whether only through HTTP requests (true/false)
  • Secure: Whether to request only through HTTPS (true/false)
  • SameSite: Whether to restrict third-party cookies
  • Priority: Priority, with low, medium (default) or high values

add cookie

The add operation only accepts a defined set of serializable JSONobjects.

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.example.com")

# Adds the cookie into current browser context
driver.add_cookie({
    
    "name": "foo", "value": "bar"})

get cookie

According to nameobtain a single cookie.

driver.add_cookie({
    
    "name": "foo", "value": "bar"})
print(driver.get_cookie("foo"))

"""
{'domain': 'example.com', 'httpOnly': False, 'name': 'foo', 'path': '/', 'secure': False, 'value': 'bar'}
"""

Get all cookies.

driver.add_cookie({
    
    "name": "test1", "value": "cookie1"})
driver.add_cookie({
    
    "name": "test2", "value": "cookie2"})
print(driver.get_cookies())

"""
[
    {'domain': 'example.com', 'httpOnly': False, 'name': 'test2', 'path': '/', 'secure': False, 'value': 'cookie2'}, 
    {'domain': 'example.com', 'httpOnly': False, 'name': 'test1', 'path': '/', 'secure': False, 'value': 'cookie1'}
]
"""

delete cookies

Delete a single cookie based on name.

driver.add_cookie({
    
    "name": "test1", "value": "cookie1"})
driver.add_cookie({
    
    "name": "test2", "value": "cookie2"})
driver.delete_cookie("test1")
print(driver.get_cookies())

"""
[
	{'domain': 'example.com', 'httpOnly': False, 'name': 'test2', 'path': '/', 'secure': False, 'value': 'cookie2'}
]
"""

Delete all cookies.

driver.add_cookie({
    
    "name": "test1", "value": "cookie1"})
driver.add_cookie({
    
    "name": "test2", "value": "cookie2"})
driver.delete_all_cookies()
print(driver.get_cookies())

"""
[]
"""

cookie saving and reading

In the project, usually cookiesave to a file and read it directly at the calling place.

"""
cookies 保存到文件
"""
driver.get("已经登录后的网站")
cookies = driver.get_cookies()
with open("cookies.yaml", "w", encoding="utf-8") as f:
	yaml.dump(cookies, f)

"""
使用 cookies 时从文件进行读取
"""
driver.get("重新打开一个未登录的窗口")
with open("cookies.yaml", encoding="utf-8") as f:
    cookies = yaml.safe_load(f)
for cookie in cookies:
    driver.add_cookie(cookie)

SameSite

SameSite is used to restrict third-party Cookieattributes, this setting prevents CSRF(cross-site request forgery) attacks and user tracking.

Use restrictions:

  • chrome(80+version)
  • Firefox(79+version)
  • Selenium v4+

Property settings:

  • Strict: Strict mode, completely forbidden to carry and send cookieswith third-party website requests
  • Lax: loose mode, allowing cookiesportability GETto be sent together with requests from third-party websites
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.example.com")
# 设置方式
driver.add_cookie({
    
    "name": "foo1", "value": "value", 'sameSite': 'Strict'})
driver.add_cookie({
    
    "name": "foo2", "value": "value", 'sameSite': 'Lax'})
cookie1 = driver.get_cookie('foo1')
cookie2 = driver.get_cookie('foo2')
print(cookie1)
print(cookie2)  

Guess you like

Origin blog.csdn.net/lan_yangbi/article/details/127968317