You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCP Python虚拟环境中Selenium无法正常运行问题求助

GCP虚拟机中Selenium启动Chrome报错解决方案

问题场景

  • 环境:GCP共享虚拟机(非root用户,与其他2位用户共用)
  • 操作步骤:
    1. 搭建新环境(未使用sudo)
    2. 下载与Chrome 108匹配的Chromedriver,执行chmod a+x赋予执行权限
    3. 运行prova.py脚本:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
d = webdriver.Chrome('/home/mattiasospetti5/chromedriver',chrome_options=chrome_options)
d.get('https://www.google.com/')
  • 执行python prova.py后报错:
File "/home/mattiasospetti5/tutorial-env/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 106, in __init__
    super().__init__(
  File "/home/mattiasospetti5/tutorial-env/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 288, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/mattiasospetti5/tutorial-env/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 381, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/mattiasospetti5/tutorial-env/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "/home/mattiasospetti5/tutorial-env/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

可行解决方案

1. 补充Chrome启动参数

DevToolsActivePort报错多因端口或临时目录权限问题,添加以下参数:

chrome_options.add_argument('--remote-debugging-port=9222')
chrome_options.add_argument('--user-data-dir=/tmp/chrome_user_data')
  • --remote-debugging-port:指定固定调试端口,避免自动分配端口失败
  • --user-data-dir:指定/tmp下的自定义用户目录,确保普通用户有读写权限

同时注意:若你的Selenium版本≥4.0,webdriver.Chrome的参数名已从chrome_options改为options,修改后的完整脚本:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--remote-debugging-port=9222')
chrome_options.add_argument('--user-data-dir=/tmp/chrome_user_data')
d = webdriver.Chrome('/home/mattiasospetti5/chromedriver', options=chrome_options)
d.get('https://www.google.com/')

2. 验证Chrome基础运行权限

先直接在终端执行Chrome命令,确认普通用户能启动:

/usr/bin/google-chrome --headless --no-sandbox --disable-dev-shm-usage --remote-debugging-port=9222 https://google.com

若此命令报错,说明Chrome本身无法在普通用户下运行:

  • 可联系管理员调整Chrome安装目录的权限
  • 若Chrome未安装在/usr/bin,手动指定二进制文件路径:
chrome_options.binary_location = '/path/to/your/google-chrome'

3. 确认Chromedriver与Chrome版本严格匹配

再次验证版本一致性:

# 查看Chromedriver版本
/home/mattiasospetti5/chromedriver --version
# 查看Chrome版本
google-chrome --version

确保两者版本号完全一致(如均为108.0.5359.xx),小版本不匹配也会导致启动失败。

4. 减少资源占用

添加--disable-gpu参数降低资源消耗,避免因虚拟机资源不足崩溃:

chrome_options.add_argument('--disable-gpu')

内容的提问来源于stack exchange,提问作者elbarto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 15:40:48