修改Python版Selenium 4.16.0的默认缓存路径(Windows 10环境)
修改Windows 10下Python版Selenium 4.16.0的默认缓存路径
你可以通过配置对应浏览器的启动选项,指定自定义缓存目录来解决冲突问题,以下是主流浏览器的实现方式:
Chrome浏览器
通过ChromeOptions添加参数,可选择指定包含缓存的用户数据目录,或单独指定缓存路径:
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() # 方式1:指定用户数据目录(包含缓存、书签等所有用户数据) chrome_options.add_argument(r"--user-data-dir=C:\your\custom\chrome_data_dir") # 方式2:仅单独指定缓存目录 chrome_options.add_argument(r"--disk-cache-dir=C:\your\custom\chrome_cache_dir") driver = webdriver.Chrome(options=chrome_options)
Firefox浏览器
通过FirefoxOptions设置缓存目录偏好:
from selenium import webdriver from selenium.webdriver.firefox.options import Options firefox_options = Options() # 设置磁盘缓存的父目录 firefox_options.set_preference("browser.cache.disk.parent_directory", r"C:\your\custom\firefox_cache_dir") driver = webdriver.Firefox(options=firefox_options)
Edge浏览器
配置逻辑与Chrome一致,使用EdgeOptions添加对应参数:
from selenium import webdriver from selenium.webdriver.edge.options import Options edge_options = Options() # 方式1:指定用户数据目录 edge_options.add_argument(r"--user-data-dir=C:\your\custom\edge_data_dir") # 方式2:仅指定缓存目录 edge_options.add_argument(r"--disk-cache-dir=C:\your\custom\edge_cache_dir") driver = webdriver.Edge(options=edge_options)
注意事项
- 自定义路径必须是目录路径,不能指向已存在的文件,且当前用户需拥有该目录的读写权限
- 若指定的目录不存在,Selenium会自动创建该目录
- 不同浏览器的参数/配置项不可混用
内容的提问来源于stack exchange,提问作者trogne
相关产品推荐
相关产品推荐

