我需要自动从我的网络服务器下载 csv 文件。 我正在尝试使用 php 的 selenium 来完成此任务。 我编写了这个脚本,它在我的本地计算机上运行良好,它运行,登录到我的管理面板并下载文件,没有任何错误。 但是当我将其上传到我的网络服务器(我想将其作为 cronjob 运行)时,我收到以下错误:
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
供应商文件夹的路径正确。我用谷歌搜索了几个小时,已经尝试过但没有结果:
所以,我没有主意,需要一些帮助。 这是我的代码:
<?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();
对我来说,这是因为 chromedriver 位于 php 无法访问的文件夹(Program Files)中。我把它放在与 php 脚本相同的目录中,并且代码运行良好:
$chromeOptions = new ChromeOptions() ;
$chromeOptions->addArguments(['--headless=new']);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions);
$service = new ChromeDriverService('./chromedriver.exe', 9515) ;
$driver = ChromeDriver::start($capabilities, $service);