我正在PHP中使用laravel Framework并正在研究webdriver,但最近我遇到了一个错误, 可以看看我的错误吗?
我从https://packagist.org安装了facebook / webdriver,我像这样在git bash中执行。
composer require facebook / webdriver
进入Java网站并安装Java之后,下载selenium-server-standalone-3.14.0并将其放在laravel myproject文件夹中
我已经在git bash中执行了以下代码。
java -jar selenium-server-standalone-3.14.0.jar
然后我在laravel项目上设置url,并将下面的示例代码写在控制器上
<?php
namespace Facebook\WebDriver;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
class TestController extends Controller
{
public function index()
{
require_once('../vendor/autoload.php');
// start Chrome with 5 second timeout
$host = 'http://localhost:4444'; // this is the default
$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
// navigate to 'http://www.seleniumhq.org/'
$driver->get('https://www.seleniumhq.org/');
// adding cookie
$driver->manage()->deleteAllCookies();
$cookie = new Cookie('cookie_name', 'cookie_value');
$driver->manage()->addCookie($cookie);
$cookies = $driver->manage()->getCookies();
print_r($cookies);
// click the link 'About'
$link = $driver->findElement(
WebDriverBy::id('menu_about')
);
$link->click();
// wait until the page is loaded
$driver->wait()->until(
WebDriverExpectedCondition::titleContains('About')
);
// print the title of the current page
echo "The title is '" . $driver->getTitle() . "'\n";
// print the URI of the current page
echo "The current URI is '" . $driver->getCurrentURL() . "'\n";
// write 'php' in the search box
$driver->findElement(WebDriverBy::id('q'))
->sendKeys('php') // fill the search box
->submit(); // submit the whole form
// wait at most 10 seconds until at least one result is shown
$driver->wait(10)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::className('gsc-result')
)
);
// close the browser
$driver->quit();
}
}
最后,我访问浏览器并遇到以下错误
JSON decoding of remote response failed. Error code: 4 The response:
您能给我一些有关为什么发生错误的建议吗?
我的英语水平很低。因此,请理解我的错误,并感谢您阅读本文。
答案 0 :(得分:0)
您可以检查
也许您需要将主机设置为类似'http://localhost:4444/wd/hub'之类的东西,或者库可能有问题。
此外,您可以尝试在启动硒服务器时对其进行访问并尝试对其进行调试。
谢谢