这可以通过cookies.sqlite数据库 http://fileformats.archiveteam.org/wiki/Firefox_cookie_database在分配给的配置文件中webdriver
. cookies.sqlite
可以使用以下命令从 Selenium cookie 生成:
from collections.abc import Sequence
from contextlib import closing
from typing import Any, Mapping
import os
import pickle
import sqlite3
# Generated via `sqlite3 -readonly path/to/fxprofile/cookies.sqlite ".schema"`.
# See:
# - http://fileformats.archiveteam.org/wiki/Firefox_cookie_database
# - https://kb.mozillazine.org/Profile_folder_-_Firefox#Finding_the_profile_folderSQL_STATEMENT_CREATE_TABLE
SQL_STATEMENT_CREATE_TABLE = r"""
CREATE TABLE
moz_cookies (
id INTEGER PRIMARY KEY,
originAttributes TEXT NOT NULL DEFAULT '',
name TEXT,
value TEXT,
host TEXT,
path TEXT,
expiry INTEGER,
lastAccessed INTEGER,
creationTime INTEGER,
isSecure INTEGER,
isHttpOnly INTEGER,
inBrowserElement INTEGER DEFAULT 0,
sameSite INTEGER DEFAULT 0,
rawSameSite INTEGER DEFAULT 0,
schemeMap INTEGER DEFAULT 0,
CONSTRAINT moz_uniqueid UNIQUE (name, host, path, originAttributes)
)
"""
SQL_STATEMENT_INSERT_COOKIE = r"""
INSERT INTO
moz_cookies (
name,
value,
host,
path,
expiry,
lastAccessed,
creationTime,
isSecure,
isHttpOnly
)
VALUES (
:name,
:value,
:domain,
:path,
:expiry,
:access_time,
:creation_time,
:secure,
:httponly
);
"""
def generate_cookies_db(
cookies: Sequence[Mapping[str, Any]],
access_time: int,
creation_time: int,
db_path: str | os.PathLike,
) -> None:
with closing(sqlite3.connect(db_path)) as connection:
with closing(connection.cursor()) as cursor:
cursor.execute(SQL_STATEMENT_CREATE_TABLE)
for cookie in cookies:
values = {
"access_time": access_time,
"creation_time": creation_time,
"domain": cookie.get("domain"),
"expiry": cookie.get("expiry"),
"httponly": cookie.get("httpOnly", False),
"name": cookie["name"],
"path": cookie.get("path"),
"secure": cookie.get("secure", False),
"value": cookie["value"],
}
cursor.execute(SQL_STATEMENT_INSERT_COOKIE, values)
connection.commit()
def main():
cookies_path = "cookies.pkl"
with open("cookies.pkl", "rb") as fp:
cookies = pickle.load(fp)
access_time_us = int(os.path.getmtime(cookies_path) * 1_000_000)
creation_time_us = int(os.path.getctime(cookies_path) * 1_000_000)
generate_cookies_db(
cookies, access_time_us, creation_time_us, "./cookies.sqlite"
)
if __name__ == "__main__":
main()
或者,您可以从现有的个人资料 https://kb.mozillazine.org/Profile_folder_-_Firefox#Finding_the_profile_folder直接与sqlite3
:
$ sqlite3 -readonly /path/to/fxprofile/cookies.sqlite ".mode insert moz_cookies" ".schema" "SELECT * FROM moz_cookies WHERE host LIKE '%.example.com'" | sqlite3 ./cookies.sqlite
注意cookies.sqlite
Firefox 运行时将被锁定。
然后复制生成的cookies.sqlite
到使用的配置文件webdriver
:
$ mkdir ./profile_template
$ mv ./cookies.sqlite ./profile_template
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.profile = webdriver.FirefoxProfile("./profiletemplate")
with webdriver.Firefox(options=options) as session:
session.get("https://example.com")
该目录将被 Selenium 视为只读,因为webdriver.FirefoxProfile没有坚持下去 https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.firefox_profile.