首页 > 解决方案 > 我该如何解决这个关于 webdriver 的错误?

问题描述

我在 PHP 中使用 laravel 框架并学习 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: 

你能给我一些关于为什么会发生错误的建议吗?

我的英语水平很低。所以请理解我的错误并感谢您阅读这篇文章。

标签: phplaravelselenium-webdriverwebdriver

解决方案


你可以检查这个

php-webdriver/问题/350

php-webdriver/问题/347

也许您需要将主机设置为 - ' http://localhost:4444/wd/hub ' - 或者可能是库的问题。

您也可以尝试在启动时访问 selenium 服务器并尝试对其进行调试。

谢谢,


推荐阅读