Selenium + php-webdriverで両方のバージョンを上げたら、
PHP Fatal error: Uncaught Facebook\WebDriver\Exception\Internal\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"capabilities":{"firstMatch":[{"browserName":"chrome","platformName":"linux","goog:chromeOptions":{"binary":"/usr/bin/google-chrome","args":["--headless","--disable-gpu","--no-sandbox"]}}]},"desiredCapabilities":{"browserName":"chrome","platform":"Linux","goog:chromeOptions":{"binary":"/usr/bin/google-chrome","args":["--headless","--disable-gpu","--no-sandbox"]}}}
Operation timed out after 180000 milliseconds with 0 bytes received in <http://example.com/job/soycmstest/ws/vendor/php-webdriver/webdriver/lib/Exception/Internal/WebDriverCurlException.php>:20
Stack trace:
/** 以下省略 **/
のエラーに陥ってしまった。
このエラーを解決した時にしたことをメモとして残しておく。
はじめに環境を記載しておく
・Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-84-generic x86_64)
・Selenium 4.13.0 (Seleniumの3系からアップグレード)
・php-webdriver 1.15.0 (1.12系からアップグレード)
・PHP 8.2.10
・Apache 2.4.57
テストコードの背景は、ログインを要するWebアプリで、何度もログインとログアウトを繰り返す。
同じような処理が続くので、
<?php
class WebDriverUtil {
private $driver;
function init(){
$host = "http://example.com:4444/wd/hub";
$caps = DesiredCapabilities::chrome();
$opts = new ChromeOptions();
$opts->setBinary("/usr/bin/google-chrome");
$opts->addArguments(["--headless","--disable-gpu", "--no-sandbox"]);
$caps->setCapability(ChromeOptions::CAPABILITY, $opts);
$caps->setCapability("applicationCacheEnabled", false);
$caps->setPlatform("Linux");
$this->driver = RemoteWebDriver::create($host, $caps);
}
function login(){
self::init();
$this->driver->get("http://example.com/login");
//ログインに関する処理
}
function logout(){
//ログアウトに関する処理
$this->driver->close();
}
}
のようなクラスを用意し、loginとlogoutを繰り返す。
Seleniumのアップグレード前はログインを何度も行うことができたが、アップグレード後は大体三回目のログインで冒頭のエラーになる。
Stack traceには
Facebook\WebDriver\Remote\RemoteWebDriver::create()
の時にエラーになることがわかったので、login内のinitの実行を初回のみにしてみたところ、
PHP Fatal error: Uncaught Facebook\WebDriver\Exception\InvalidSessionIdException: invalid session id in <http://example.com/job/soycmstest/ws/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php>:110
のエラーに変わってしまった。
どうやら、ログイン毎にセッションを破棄して、再生成する必要があるようだ。
セッションに有効であるか?わからないけれども、php-webdriverが用意しているquitメソッドを用いると良いらしいので、
<?php
class WebDriverUtil {
private $driver;
function init(){
if($this->driver instanceof Facebook\WebDriver\Remote\RemoteWebDriver){
$this->driver->quit();
}
$host = "http://example.com:4444/wd/hub";
$caps = DesiredCapabilities::chrome();
$opts = new ChromeOptions();
$opts->setBinary("/usr/bin/google-chrome");
$opts->addArguments(["--headless","--disable-gpu", "--no-sandbox"]);
$caps->setCapability(ChromeOptions::CAPABILITY, $opts);
$caps->setCapability("applicationCacheEnabled", false);
$caps->setPlatform("Linux");
$this->driver = RemoteWebDriver::create($host, $caps);
}
function login(){
self::init();
$this->driver->get("http://example.com/login");
//ログインに関する処理
}
function logout(){
//ログアウトに関する処理
$this->driver->close();
}
}
RemoteWebDriver::create($host, $caps)の前にdriverをquitしてみたら、冒頭のエラーは発生しなくなった。





