WebDriver 异常:无法找到 (POST) /wd/hub/session 的处理程序
WebDriver Exception: Unable to find handler for (POST) /wd/hub/session
Google Chrome 79.0.3945.130
Chrome驱动程序 79.0.3945.36
selenium-server-4.0.0-alpha-4.jar
来自 the php-webdriver GitHub as of 1/22/2020
的最新代码
我在本地主机上使用 Xfvb 以独立模式启动 Selenium 服务器:
$ Xvfb :99 -screen 5 1920x1080x8 &
$ export DISPLAY=:99
$ java -Dwebdriver.chrome.driver="./chromedriver" -jar selenium-server-4.0.0-alpha-4.jar standalone
然后我有一个测试助手 class,它在 PHP 代码中启动:
1 final public static function createWebDriver() {
2 $options = new ChromeOptions();
3 $options->addArguments(array('--window-size=1920,1080'));
4 $caps= DesiredCapabilities::chrome();
5 $caps->setCapability(ChromeOptions::CAPABILITY, $options);
6 $driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);
7 $driver->manage()->window()->maximize();
8 return $driver;
9}
当我 运行 测试并调用 RemoteWebDriver::create() 函数时,它抛出异常:
Facebook\WebDriver\Exception\UnknownCommandException: Unable to find
handler for (POST) /wd/hub/session
/home/me/UnitTest/vendor/facebook/webdriver/lib/Exception/WebDriverException.php:137
/home/me/UnitTest/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:380
/home/me/UnitTest/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:136
/var/www/html/project/core/functions/SeleniumTestHelper.php:6
问题所在的行是:
$driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);
我用 netstat 确认我正在侦听端口 4444 的所有地址。我在我的机器上找不到名为 "hub" 的目录。我不确定为什么这不起作用,而且似乎没有比这个例外更多的信息让我离开。
andrewnichols 帮助我解决了 GitHub!这是他的回应。非常感谢他。我将在这里为遇到此问题的任何人分享回复。
嗨@JoeyofBladez,
Selenium 4 改变了 URL。它不再落后 /wd/hub。错误是正确的。它不知道 /wd/hub/session 是什么意思。新的 URL 是 http://localhost:4444 which would make the new session URL http://localhost:4444/session.
查看状态
我们扩展了 php-webdriver README 以明确您应该为哪个版本使用哪个 URL 的 Selenium 服务器。
Chrome 驱动程序
如果您在没有任何选项的命令行上启动 Chromedriver:
$ chromedriver
然后你使用:
$serverUrl = 'http://localhost:9515';
$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome());
但是您可以自己指定端口:
$ chromedriver --port=4444
然后你使用:
$serverUrl = 'http://localhost:4444';
GeckoDriver
$ geckodriver
在您的脚本中使用:
$serverUrl = 'http://localhost:4444';
$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::firefox());
Selenium 独立服务器
// selenium-server-standalone-#.jar (version 2.x or 3.x)
$serverUrl = 'http://localhost:4444/wd/hub';
// selenium-server-standalone-#.jar (version 4.x)
$serverUrl = 'http://localhost:4444';
$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome()); // or other browser
Google Chrome 79.0.3945.130
Chrome驱动程序 79.0.3945.36
selenium-server-4.0.0-alpha-4.jar
来自 the php-webdriver GitHub as of 1/22/2020
我在本地主机上使用 Xfvb 以独立模式启动 Selenium 服务器:
$ Xvfb :99 -screen 5 1920x1080x8 &
$ export DISPLAY=:99
$ java -Dwebdriver.chrome.driver="./chromedriver" -jar selenium-server-4.0.0-alpha-4.jar standalone
然后我有一个测试助手 class,它在 PHP 代码中启动:
1 final public static function createWebDriver() {
2 $options = new ChromeOptions();
3 $options->addArguments(array('--window-size=1920,1080'));
4 $caps= DesiredCapabilities::chrome();
5 $caps->setCapability(ChromeOptions::CAPABILITY, $options);
6 $driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);
7 $driver->manage()->window()->maximize();
8 return $driver;
9}
当我 运行 测试并调用 RemoteWebDriver::create() 函数时,它抛出异常:
Facebook\WebDriver\Exception\UnknownCommandException: Unable to find handler for (POST) /wd/hub/session /home/me/UnitTest/vendor/facebook/webdriver/lib/Exception/WebDriverException.php:137 /home/me/UnitTest/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:380 /home/me/UnitTest/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:136 /var/www/html/project/core/functions/SeleniumTestHelper.php:6
问题所在的行是:
$driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);
我用 netstat 确认我正在侦听端口 4444 的所有地址。我在我的机器上找不到名为 "hub" 的目录。我不确定为什么这不起作用,而且似乎没有比这个例外更多的信息让我离开。
andrewnichols 帮助我解决了 GitHub!这是他的回应。非常感谢他。我将在这里为遇到此问题的任何人分享回复。
嗨@JoeyofBladez,
Selenium 4 改变了 URL。它不再落后 /wd/hub。错误是正确的。它不知道 /wd/hub/session 是什么意思。新的 URL 是 http://localhost:4444 which would make the new session URL http://localhost:4444/session.
查看状态我们扩展了 php-webdriver README 以明确您应该为哪个版本使用哪个 URL 的 Selenium 服务器。
Chrome 驱动程序
如果您在没有任何选项的命令行上启动 Chromedriver:
$ chromedriver
然后你使用:
$serverUrl = 'http://localhost:9515';
$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome());
但是您可以自己指定端口:
$ chromedriver --port=4444
然后你使用:
$serverUrl = 'http://localhost:4444';
GeckoDriver
$ geckodriver
在您的脚本中使用:
$serverUrl = 'http://localhost:4444';
$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::firefox());
Selenium 独立服务器
// selenium-server-standalone-#.jar (version 2.x or 3.x)
$serverUrl = 'http://localhost:4444/wd/hub';
// selenium-server-standalone-#.jar (version 4.x)
$serverUrl = 'http://localhost:4444';
$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome()); // or other browser