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

PHP-webdriver脚本在托管服务器无法运行,chromedriver报错

PHP版Selenium(php-webdriver)服务器Cron任务运行报错解决求助

需要实现从托管服务器自动下载CSV文件的功能,采用PHP版Selenium(php-webdriver)编写脚本。脚本在本地运行正常,可成功登录后台并下载文件,但上传至服务器作为Cron任务运行时,出现以下致命错误:

PHP Fatal error:  Uncaught Facebook\WebDriver\Exception\Internal\IOException: File is not executable. Make sure the path is correct or use environment variable to specify location of the executable. ("chromedriver") in /crons/booking_scraper/vendor/php-webdriver/webdriver/lib/Exception/Internal/IOException.php:14
Stack trace:
#0 /crons/booking_scraper/vendor/php-webdriver/webdriver/lib/Remote/Service/DriverService.php(138): Facebook\WebDriver\Exception\Internal\IOException::forFileError()
#1 /crons/booking_scraper/vendor/php-webdriver/webdriver/lib/Remote/Service/DriverService.php(50): Facebook\WebDriver\Remote\Service\DriverService->setExecutable()
#2 /crons/booking_scraper/vendor/php-webdriver/webdriver/lib/Chrome/ChromeDriverService.php(35): Facebook\WebDriver\Remote\Service\DriverService->__construct()
#3 /crons/booking_scraper/vendor/php-webdriver/webdriver/lib/Chrome/ChromeDriver.php(27): in /crons/booking_scraper/vendor/php-webdriver/webdriver/lib/Exception/Internal/IOException.php on line 14

已确认vendor目录路径正确,尝试过以下方法但均无效:

  • 删除vendor目录后重新执行composer update
  • 切换使用RemoteWebDriver与ChromeDriver(本地均正常)
  • 改用Python版Selenium(本地正常但服务器无法运行Python)
  • 改用Node.js+Puppeteer(本地正常但服务器报错)

附上PHP脚本代码:

<?php

namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\Chrome\ChromeOptions;

require_once('vendor/autoload.php');

// Chromedriver (if started using --port=9515 as above)
$serverUrl = 'http://localhost:9515';
$urls = ['link-to-the-login-form', 'link-to-the-file-to-be-downloaded'];
$pw = 'my-passw';
$user_name = 'my-username';

$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments(['--headless']);
$chromeOptions->setExperimentalOption(
    'prefs', 
    ['download.default_directory' => "./"]
);

$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions);

// Start the browser with $capabilities
$driver = RemoteWebDriver::create($serverUrl, $capabilities);

// Go to URL
$driver->get($urls[0]);
// $driver->manage()->window()->maximize();

$driver->findElement(WebDriverBy::name("username"))
    ->sendKeys($user_name);

$driver->findElement(WebDriverBy::name("password"))
    ->sendKeys($pw);

$driver->findElement(WebDriverBy::name("login"))
    ->click();

echo 'about to download the bookings.csv for you.'.PHP_EOL;
$driver->get($urls[1]);

echo 'ready!'.PHP_EOL;

// give 6sec to download the file before closing the browser
sleep(6);

// Make sure to always call quit() at the end to terminate the browser session
$driver->quit();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 09:04:56