php - 我该如何解决这个关于 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:
你能给我一些关于为什么会发生错误的建议吗?
我的英语水平很低。所以请理解我的错误并感谢您阅读这篇文章。
解决方案
你可以检查这个
也许您需要将主机设置为 - ' http://localhost:4444/wd/hub ' - 或者可能是库的问题。
您也可以尝试在启动时访问 selenium 服务器并尝试对其进行调试。
谢谢,
推荐阅读
- android - 当使用空格时,flutter-autocomplete-textfield 不提示
- c++ - CString.Format 以相同的精度生成不同的值
- python - Discord.py 在脚本中运行 2 个机器人实例
- mysql - mySQL 过程不会在提交语句上提交
- python - BeautifulSoup4 - 请求 - 如何找到 TBODY 类?
- angular - 我无法获取图像对象角度文件上传器库
- google-cloud-storage - 无法在 Google Cloud Storage 存储桶中加载 Adobe Captivate HTML5 演示文稿
- arrays - 如何使用 $addFields 在 MongoDB 中创建数组?
- python-3.x - Pandas 数据框使用列逻辑对行进行重复数据删除
- azure - Azure AD“组”声明未传递给 mod_auth_openidc