まず、本家から。
[selenium]
https://www.selenium.dev/downloads/
[php-webdriver]
https://github.com/php-webdriver/php-webdriver
[php-webdriver api reference]
https://php-webdriver.github.io/php-webdriver/latest/index.html
[chrome driver]
https://chromedriver.chromium.org/downloads
日本語フォントのインストール
まず初めに日本語フォントのインストールを行う。これがないと文字化けしまくる。
$ sudo apt install fonts-takao
$ sudo fc-cache -fv
$ fc-match TakaoGothic
TakaoGothic.ttf: "TakaoGothic" "Regular"
$ sudo apt install fonts-ipafont fonts-ipaexfont
$ sudo fc-cache -fv
$ fc-match IPAGothic
ipag.ttf: "IPAGothic" "Regular"
chromeのインストール
まずaptの認証キーを登録する。
$ wget https://dl.google.com/linux/linux_signing_key.pub
$ sudo apt-key add linux_signing_key.pub
次にaptのソースリストに追加し、chromeをインストール。
$ echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list
$ sudo apt-get update
$ sudo apt-get install google-chrome-stable
インストールされたか確認する。
$ google-chrome --version
Google Chrome 88.0.4324.182
chromedriverのインストール
次にchromedriveをインストールする。注意点としては、インストールしたchromeと同じバージョンのものを使う事。どれがどれに対応しているかの詳細は本家に書いてあるのでそちらを参照。
$ wget https://chromedriver.storage.googleapis.com/88.0.4324.96/chromedriver_linux64.zip
$ unzip chromedriver_linux64.zip
$ sudo mv chromedriver /usr/local/bin/
インストールされたか確認する。
$ chromedriver --version
ChromeDriver 88.0.4324.96 (68dba2d8a0b149a1d3afac56fa74648032bcf46b-refs/branch-heads/4324@{#1784})
Seleniumのインストール
seleniumを取得し、インストールする。
$ wget https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar
$ sudo cp selenium-server-standalone-3.141.59.jar /usr/local/bin/
起動にはjavaが必要なので、入っていなければインストールする。
$ sudo apt install default-jre
起動してみる。
$ java -jar /usr/local/bin/selenium-server-standalone-3.141.59.jar &
composerのインストール
composerが入っていない場合はインストールする。入っている場合はここは不要。
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
$ composer --version
Composer version 2.0.9 2021-01-27 16:09:27
php-webdriverのインストール
php-webdriverをcomposerでインストールする。もともとはfacebook/webdriverという名前だったらしいが、1.7ぐらいで変更された模様。基本的な機能は同じと思われるが、アップデートする場合は自己責任で。
$ mkdir sample
$ cd sample/
$ composer require php-webdriver/webdriver
Using version ^1.10 for php-webdriver/webdriver
./composer.json has been created
Running composer update php-webdriver/webdriver
Loading composer repositories with package information
Updating dependencies
Lock file operations: 4 installs, 0 updates, 0 removals
- Locking php-webdriver/webdriver (1.10.0)
- Locking symfony/polyfill-mbstring (v1.22.1)
- Locking symfony/polyfill-php80 (v1.22.1)
- Locking symfony/process (v5.2.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
- Downloading symfony/polyfill-php80 (v1.22.1)
- Downloading symfony/process (v5.2.3)
- Downloading symfony/polyfill-mbstring (v1.22.1)
- Downloading php-webdriver/webdriver (1.10.0)
- Installing symfony/polyfill-php80 (v1.22.1): Extracting archive
- Installing symfony/process (v5.2.3): Extracting archive
- Installing symfony/polyfill-mbstring (v1.22.1): Extracting archive
- Installing php-webdriver/webdriver (1.10.0): Extracting archive
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
3 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
動作確認のためサンプルコードを作成。
$ vi sample.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Chrome\ChromeDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\WebDriverBy;
$driverPath = realpath("/usr/local/bin/chromedriver");
putenv("webdriver.chrome.driver=" . $driverPath);
// chrome option
$options = new ChromeOptions();
$options->addArguments([
'disable-infobars',
'--headless',
//'start-maximized',
'window-size=1920,1600',
]);
$capabilitites = DesiredCapabilities::chrome();
$capabilitites->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = ChromeDriver::start($capabilitites);
// googleのページ取得
$driver->get('https://www.google.co.jp/');
// 表示されるまで待つ
$driver->wait(2)->until(
WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::name('q'))
);
// フォームに文字列を入力して検索実行
$element = $driver->findElement(WebDriverBy::name('q'))
->sendKeys('うっかりさん 困った時の備忘録')
->submit();
// 表示されるまで待つ
$driver->wait(3)->until(WebDriverExpectedCondition::titleContains('うっかりさん'));
// キャプチャをとる
$file = "./sample.png";
$driver->takeScreenshot($file);
// ブラウザを閉じる
$driver->quit();
実行してみる。
$ php -v
PHP 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
$ php sample.php
キャプチャがとれていれば成功。

コメント