Docker环境下Symfony Panther调用ChromeDriver报错求助
Symfony Panther 在Docker容器中启动Chrome客户端失败的解决方案
环境信息
- 基础Docker镜像:
php:8.0.2-fpm-buster - Composer依赖配置:
"symfony/panther": "^2.0", "require-dev": { "dbrekelmans/bdi": "^1.0" } - 已执行命令:
composer require --dev dbrekelmans/bdi && vendor/bin/bdi - 容器内浏览器版本:
root@c2392b7b5478:/usr/src/app# chromium -version Chromium 90.0.4430.212 built on Debian 10.9, running on Debian 10.8 root@c2392b7b5478:/usr/src/app# google-chrome-stable -version Google Chrome 106.0.5249.103
问题1:ChromeDriver未找到
执行代码:
$httpClient = \Symfony\Component\Panther\Client::createChromeClient();
报错信息:
RuntimeException {#1085 ▼ #message: ""chromedriver" binary not found. Install it using the package manager of your operating system or by running "composer require --dev dbrekelmans/bdi && vendor/b ▶" }
问题2:Chrome启动崩溃(更新后)
下载对应Chrome版本的ChromeDriver后,执行指定路径的代码:
$httpClient = \Symfony\Component\Panther\Client::createChromeClient( $this->kernel->getProjectDir().'/bin/chromedriver' );
报错信息:
Facebook\WebDriver\Exception\UnknownErrorException {#988 ▼ -results: array:1 [▼ "value" => array:3 [▼ "error" => "unknown error" "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. 确保ChromeDriver与Chrome版本匹配并配置可执行权限
- 确认ChromeDriver版本与Chrome主版本一致(当前Chrome为106.0.5249.103,对应ChromeDriver版本为106.0.5249.61)
- 将ChromeDriver移动到系统可执行路径:
mv /usr/src/app/bin/chromedriver /usr/local/bin/ - 赋予执行权限:
chmod +x /usr/local/bin/chromedriver
2. 添加Chrome必要启动参数
Docker环境中运行Chrome必须禁用沙箱、启用无头模式,并解决/dev/shm空间不足问题,修改代码如下:
use Symfony\Component\Panther\Client; use Facebook\WebDriver\Chrome\ChromeOptions; $chromeOptions = new ChromeOptions(); $chromeOptions->addArguments([ '--headless=new', '--no-sandbox', '--disable-dev-shm-usage', '--disable-gpu', '--remote-debugging-port=9222', ]); // 无需指定ChromeDriver路径,系统可执行路径已配置 $httpClient = Client::createChromeClient(null, $chromeOptions);
3. 重新运行bdi工具检测驱动
执行以下命令确保bdi正确识别并安装驱动:
vendor/bin/bdi detect drivers
执行完成后,检查vendor/bin/目录下是否生成了chromedriver的软链接。
4. 安装Chrome运行依赖
Debian容器中可能缺少Chrome运行所需的系统依赖,执行以下命令安装:
apt-get update && apt-get install -y \ libx11-xcb1 \ libxcb-dri3-0 \ libdrm2 \ libxcomposite1 \ libxdamage1 \ libxfixes3 \ libxrandr2 \ libgbm1 \ libasound2 \ fonts-liberation
内容的提问来源于stack exchange,提问作者Developer
相关产品推荐
相关产品推荐

