Удаление всех cookie:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('//www.example.com')
driver.delete_all_cookies()
Удаление определенных cookie по имени:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('//www.example.com')
driver.delete_cookie('cookie_name')
Сохранение cookie в файл:
import json
from selenium import webdriver
driver = webdriver.Chrome()
cookies = driver.get_cookies()
with open('cookies.txt', 'w') as file:
json.dump(cookies, file)
Загрузка cookie из файл в браузер:
from selenium import webdriver
import json
driver = webdriver.Chrome()
with open('cookies.txt', 'r') as file:
cookies = json.load(file)
for cookie in cookies:
driver.add_cookie(cookie)
driver.get("//www.example.com")
Создание своих cookie:
from selenium import webdriver
cookie = {
'name': 'cookie_name',
'value': 'cookie_value',
'path': '/',
'secure': False
}
driver = webdriver.Chrome()
driver.add_cookie(cookie)
Проверка наличия определенных cookie в браузере:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('//www.example.com')
cookie_name = 'cookie_name'
cookie = driver.get_cookie(cookie_name )
if cookie:
print(f"Cookie '{cookie_name}' found: {cookie}")
else:
print(f"Cookie '{cookie_name}' not found.")