我怎么能修复关于webdriver的这个错误?

问题描述 投票:-1回答:1

我在PHP中使用laravel Framework并学习webdriver但最近我收到错误,你能看看我的错误吗?

我从https://packagist.org安装了facebook / webdriver,我在git bash中执行,就像这样。

composer require facebook / webdriver

进入Java Web并安装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: 

你能否就错误发生的原因给我一些建议?

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

php laravel selenium-webdriver webdriver
1个回答
0
投票

你可以检查一下

php-webdriver/issues/350

php-webdriver/issues/347

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

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

谢谢,

© www.soinside.com 2019 - 2024. All rights reserved.