laravelでseleniumを動かしてみる所まで。
seleniumのインストールなどはubuntu編かcentos編をみてね。
laravelのインストール
最新版(この時点ではlaravel8)をインストール。
$ composer global require "laravel/installer=~1.1"
(長いのでログは省略)
プロジェクトを作成
laravelのプロジェクトを作成する。今回はプロジェクトのディレクトリをユーザのホームディレクトリに作成し、プロジェクト名はselenium-sampleとしている。
$ cd ~/
$ composer create-project laravel/laravel selenium-sample --prefer-dist
Creating a "laravel/laravel" project at "./selenium-sample"
Installing laravel/laravel (v8.5.12)
- Installing laravel/laravel (v8.5.12): Downloading (100%)
:
Application key set successfully.
$ cd selenium-sample/
$ php artisan -V
Laravel Framework 8.30.0
php-webdriverのインストール
composerでphp-webdriverを入れる。
$ composer require php-webdriver/webdriver
:
PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in /usr/share/php/Composer/DependencyResolver/RuleWatchGraph.php on line 52
今回の環境ではエラーが出てしまったので、メモリ上限をなしにして再度実行。コマンドは以下。
$ COMPOSER_MEMORY_LIMIT=-1 /usr/bin/composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing php-webdriver/webdriver (1.10.0): Downloading (100%)
php-webdriver/webdriver suggests installing ext-SimpleXML (For Firefox profile creation)
Writing lock file
:
laravelのコマンド用クラスを生成
コマンドクラスを生成する。今回の名前はSeleniumBatchとする。
$ php artisan make:command SeleniumBatch
Console command created successfully.
$ cd ~/selenium-sample/app/Console/Commands
$ ls
SeleniumBatch.php
ソースは以下。laravelのクラスである事以外は以前作成したものと同じ。
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
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;
use Facebook\WebDriver\Interactions\WebDriverActions;
use Facebook\WebDriver\WebDriverDimension;
class SeleniumBatch extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'batch:selenium-sample';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Selenium Sample Batch';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$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 = __DIR__."/sample.png";
$driver->takeScreenshot($file);
// ブラウザを閉じる
$driver->quit();
return 0;
}
}
作成したコマンドが存在するか確認後、コマンドを実行してみる。
$ cd ~/selenium-sample/
$ php artisan list | grep batch
:
batch
batch:selenium-sample Selenium Sample Batch
:
$ php artisan batch:selenium-sample
キャプチャがとれていれば完了。

コメント