php-webdriver 在window上的安装与使用教程

本文专门介绍的是Php-webdriver在window上的安装与使用教程,亲测有效,linux的也测试了,只是没有分享。

本文是在windows2019,宝塔面板,php7.4下测试

参考网址:https://www.icode9.com/content-3-1088633.html

1.查看谷歌浏览器的版本号

首先自行安装好谷歌浏览器,然后查询浏览器版本号

php-webdriver 在window上的安装与使用教程

2.下载对应谷歌浏览器驱动

http://chromedriver.storage.googleapis.com/index.html

或者https://npm.taobao.org/mirrors/chromedriver

php-webdriver 在window上的安装与使用教程

3.启动驱动,双击.exe

php-webdriver 在window上的安装与使用教程
php-webdriver 在window上的安装与使用教程

注意这个启动之后的端口号是:9515,和默认的4444不一样。

4.环境ok后,就是上代码了

详细教程:https://github.com/php-webdriver/php-webdriver

4.1首先安装composer

参考网址:https://blog.csdn.net/allway2/article/details/118388457

在 Windows 操作系统上安装 Composer 有两种方法:第一种是使用 Composer 安装程序设置,另一种是使用脚本手动安装。

  1. 使用安装程序
    a) 使用 Composer 安装程序安装程序安装 Composer 是在 Windows 操作系统上安装它的最简单方法。启动您的默认浏览器并访问https://getcomposer.org并单击“入门”按钮。在“安装 – Windows ”部分,点击“使用安装程序”选项;它将带您进入“使用安装程序”部分。
php-webdriver 在window上的安装与使用教程

b) 单击Composer-Setup.exe链接在您的设备上下载 Composer 安装程序。下载安装程序后,运行它进行安装并按照说明进行操作。

php-webdriver 在window上的安装与使用教程

c) 打开下载的 Composer-Setup 并单击“为所有用户安装”,这是安装 Composer 安装程序的推荐选项。

php-webdriver 在window上的安装与使用教程

d) 在弹出屏幕上,单击“是”以允许安装。

e) 现在,选择您的安装类型,然后单击下一步

php-webdriver 在window上的安装与使用教程

f) 现在,选择要使用的命令行 PHP 路径,选中添加 PHP 路径的框,然后单击下一步。(这个时候就选择宝塔的php7.4的路径

php-webdriver 在window上的安装与使用教程

g) Composer 设置弹出一个屏幕,提供使用代理服务器连接到 Internet 的选项。如果要使用代理服务器,请勾选复选框并输入代理 URL;如果没有,请留下它并单击Next我们将跳过此步骤,因为我们没有使用任何代理服务器来连接 Internet。

php-webdriver 在window上的安装与使用教程

h) Composer 设置已准备好安装在您的计算机上;检查您的设置,然后单击“安装”按钮。

php-webdriver 在window上的安装与使用教程

i) 安装 Composer setup 后,会弹出有关如何打开它的重要信息。阅读信息,单击下一步并在安装后进行相应操作。

php-webdriver 在window上的安装与使用教程

j) 单击完成按钮完成安装。

php-webdriver 在window上的安装与使用教程

当 Composer 安装在您的机器上后,打开命令 (cmd) 窗口,输入composer并按Enter键。如果它显示命令列表,则表示 Composer 已成功安装到您的计算机上。

php-webdriver 在window上的安装与使用教程

4.2 使用Composer安装php-webdriver/webdriver

注意:运行composer之前,需要去宝塔面板的php7.4,然后删除禁用函数putenv()

然后使用windows powershell进入到网站根目录后,运行下面的命令

composer require php-webdriver/webdriver

这个时候网站根目录会多一个vendor文件夹,里面的就是下载下来的东西

5.测试代码

下面代码为我修改的代码,可以用于测试

<?php

// An example of using php-webdriver.
// Do not forget to run composer install before. You must also have Selenium server started and listening on port 4444.

namespace Facebook\WebDriver;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

require_once('vendor/autoload.php');

// This is where Selenium server 2/3 listens by default. For Selenium 4, Chromedriver or Geckodriver, use http://localhost:4444/
$host = 'http://localhost:9515';

$capabilities = DesiredCapabilities::chrome();

// 非windows系统浏览器不提供可视化页面

    $options = new ChromeOptions();
    $options->addArguments(
        [
          //  '--no-sandbox',                        // 解决DevToolsActivePort文件不存在的报错
           // 'window-size=1080x1920',               // 指定浏览器分辨率
         //   '--disable-gpu',                       // 谷歌文档提到需要加上这个属性来规避bug
         //   '--hide-scrollbars',                   // 隐藏滚动条, 应对一些特殊页面
          //  'blink-settings=imagesEnabled=false',  // 不加载图片, 提升速度
           // '--ignore-certificate-errors',
           // '--ignore-ssl-errors',
            //'--headless',                          // 浏览器不提供可视化页面
        ]
    );
    $capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

$driver = RemoteWebDriver::create($host, $capabilities);

// navigate to Selenium page on Wikipedia
$driver->get('https://www.baidu.com/');


// close the browser
//$driver->quit();

会调用浏览器并打开百度,如果可以打开,环境就安装正常了。

下面的为别人根据官方文档修改的,也可用于测试

<?php

// An example of using php-webdriver.
// Do not forget to run composer install before. You must also have Selenium server started and listening on port 4444.

namespace Facebook\WebDriver;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

require_once('../vendor/autoload.php');

// This is where Selenium server 2/3 listens by default. For Selenium 4, Chromedriver or Geckodriver, use http://localhost:4444/
$host = 'http://localhost:4444';

$capabilities = DesiredCapabilities::chrome();

// 非windows系统浏览器不提供可视化页面
if ('WIN' !== strtoupper(substr(PHP_OS, 0, 3))) {
    $options = new ChromeOptions();
    $options->addArguments(
        [
            '--no-sandbox',                        // 解决DevToolsActivePort文件不存在的报错
            'window-size=1080x1920',               // 指定浏览器分辨率
            '--disable-gpu',                       // 谷歌文档提到需要加上这个属性来规避bug
            '--hide-scrollbars',                   // 隐藏滚动条, 应对一些特殊页面
            'blink-settings=imagesEnabled=false',  // 不加载图片, 提升速度
            '--headless',                          // 浏览器不提供可视化页面
        ]
    );
    $capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
}
$driver = RemoteWebDriver::create($host, $capabilities);

// navigate to Selenium page on Wikipedia
$driver->get('https://en.wikipedia.org/wiki/Selenium_(software)');

// write 'PHP' in the search box
$driver->findElement(WebDriverBy::id('searchInput')) // find search input element
->sendKeys('PHP') // fill the search box
->submit(); // submit the whole form

// wait until 'PHP' is shown in the page heading element
$driver->wait()->until(
    WebDriverExpectedCondition::elementTextContains(WebDriverBy::id('firstHeading'), 'PHP')
);

// print title of the current page to output
echo "The title is '" . $driver->getTitle() . "'\n";

// print URL of current page to output
echo "The current URL is '" . $driver->getCurrentURL() . "'\n";

// find element of 'History' item in menu
$historyButton = $driver->findElement(
    WebDriverBy::cssSelector('#ca-history a')
);

// read text of the element and print it to output
echo "About to click to button with text: '" . $historyButton->getText() . "'\n";

// click the element to navigate to revision history page
$historyButton->click();

// wait until the target page is loaded
$driver->wait()->until(
    WebDriverExpectedCondition::titleContains('Revision history')
);

// 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";

// delete all cookies
$driver->manage()->deleteAllCookies();

// add new cookie
$cookie = new Cookie('cookie_set_by_selenium', 'cookie_value');
$driver->manage()->addCookie($cookie);

// dump current cookies to output
$cookies = $driver->manage()->getCookies();
print_r($cookies);

// close the browser
$driver->quit();

使用教程请参考下面的文章

到此为止。

发布者:彬彬笔记,转载请注明出处:https://www.binbinbiji.com/jianzhanjiaoliu/2316.html

(7)
彬彬笔记彬彬笔记
上一篇 2022年5月30日
下一篇 2022年6月6日

相关推荐

发表回复

登录后才能评论
蜀ICP备14017386号-13