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/zh-hant/jianzhanjiaoliu/2316.html

(7)
彬彬筆記彬彬筆記
上一篇 2022年5月30日
下一篇 2022年6月6日

相關推薦

發表回復

登錄後才能評論
蜀ICP備14017386號-13