Nobara(Fedora衍生版)Linux下使用Selenium与ChromeDriver时的会话创建失败问题
Nobara(Fedora衍生版)Linux下使用Selenium与ChromeDriver时的会话创建失败问题
看起来你在Nobara Linux上用Selenium启动Chrome时碰到了典型的「用户数据目录被占用」问题,这个情况在Chromium系浏览器(Chrome、Brave都算)里特别常见,尤其是系统里残留后台进程、参数设置出错,或者Flatpak沙箱权限受限的时候。结合你的环境,我给你一步步拆解解决方案:
第一步:彻底清理Chromium系浏览器的后台进程
哪怕你关了Chrome/Brave窗口,后台可能还残留GPU进程、渲染进程在偷偷占用用户数据目录。先把这些进程全杀掉:
# 批量杀掉所有Chrome/Brave相关进程 killall chrome chrome-browser brave-browser # 如果还有漏网之鱼,用更彻底的方式清理 ps aux | grep -E "chrome|brave" | awk '{print $2}' | xargs kill -9
第二步:创建专属Selenium的Chrome用户数据目录
别用系统默认的~/.config/google-chrome(这个目录很容易被正常打开的Chrome占用),自己新建一个完全独立的目录:
# 新建独立目录 mkdir -p ~/.config/selenium-chrome-profile # 设为仅当前用户可访问的权限,避免权限冲突 chmod 700 ~/.config/selenium-chrome-profile
第三步:修正你的Selenium代码参数
看你的代码里有几个明显的小错误,我帮你调整后:
log_path是Service类的参数,不是ChromeOptions的,之前放错地方了- 删掉无效的
--path参数(ChromeOptions里没有这个选项) - 修正了注释里多打了短横线的
profile-directory参数 - 指定我们刚创建的独立用户数据目录
修改后的代码:
from selenium.webdriver.common.by import By from selenium import webdriver from selenium.webdriver.chrome.service import Service import logging options = webdriver.ChromeOptions() service = Service() # 保留你需要的日志配置 options.set_capability( "goog:loggingPrefs", {"performance": "ALL", "browser": "ALL"} ) # 把日志路径移到Service的参数里,这才是正确的位置 service.log_path = '/home/cesarin/IdeaProjects/Seleniumtest1/chromedriver_log_path.log' # 保留你需要的窗口和日志级别设置 options.add_argument("--start-maximized") options.add_argument("--log-level=3") options.add_argument("--enable-logging --v=1") # 关键:指定专属Selenium的用户数据目录,彻底避免和系统Chrome冲突 options.add_argument("--user-data-dir=/home/cesarin/.config/selenium-chrome-profile/") # 如果需要用特定子profile,可以取消注释(先确保目录存在,或者直接删掉这行) # options.add_argument("--profile-directory=Profile 1") # 如果你用的是Flatpak版Chrome,需要指定二进制文件路径(示例路径,根据你的安装调整) # options.binary_location = "/var/lib/flatpak/app/com.google.Chrome/current/active/export/bin/com.google.Chrome" # 系统原生Chrome的路径通常是 /usr/bin/google-chrome driver = webdriver.Chrome(service=service, options=options) log_entries = driver.get_log("performance")
第四步:解决Flatpak IntelliJ的沙箱权限问题
因为你用的是Flatpak版IntelliJ,它默认在沙箱里运行,可能无法访问你home目录下的自定义profile目录。给IntelliJ添加必要的访问权限:
# 简单直接的方式:允许IntelliJ访问整个home目录 flatpak override com.jetbrains.IntelliJ-IDEA-Community --filesystem=home # 更安全的方式:只允许访问需要的特定目录 flatpak override com.jetbrains.IntelliJ-IDEA-Community --filesystem=/home/cesarin/.config/selenium-chrome-profile flatpak override com.jetbrains.IntelliJ-IDEA-Community --filesystem=/home/cesarin/IdeaProjects/Seleniumtest1
第五步:验证测试
做完上面的步骤后,先确保没有任何Chrome/Brave窗口在运行,再启动你的Selenium脚本,应该就能正常打开Chrome并获取性能日志了。
如果还是有问题,可以试试:
- 检查自定义profile目录的权限,确保你的用户有读写权限
- 暂时关闭或卸载Brave,验证是否是Brave的进程在偷偷占用资源
- 手动下载对应版本的ChromeDriver,指定
Service的executable_path参数,比如:service = Service(executable_path="/path/to/your/downloaded/chromedriver")
备注:内容来源于stack exchange,提问作者Cesarin Pillin
相关产品推荐
相关产品推荐

