LoginSignup
1
0

More than 3 years have passed since last update.

WindowsでSelenium+phpを動かす方法

Posted at

本記事の目的

Windows環境でphpを使用してSeleniumuを動かすメモ

MacやLinuxを動かしたい人は下記記事を参考にしてください(本記事のベース情報)
https://qiita.com/Rasukarusan/items/0ca204d5b0f0fb876252

Seleniumとは?

Selenium は、 Webアプリケーションをテストするためのポータブルフレームワークである。 Selenium は、テストスクリプト言語を学ぶ必要なしに、機能テストを作成するための再生ツールを提供する。
参考:https://ja.wikipedia.org/wiki/Selenium_(%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%A7%E3%82%A2)

環境など

項目 versionなど ダウンロード先
OS windows
php 5.6.12 https://www.apachefriends.org/jp/index.html
composer 1.9.0 https://getcomposer.org/
java 1.8.0_281 https://java.com/ja/download/ie_manual.jsp
chromedriver 90.0.4430.24 http://chromedriver.chromium.org/downloads
selenium-server-standalone 3.141.59 https://www.selenium.dev/downloads/
  • versionは検証時で動いたものです。
  • 基本的、最新の安定版をダウンロードする。

大まかな流れ

  1. 各ソフトをダウンロード、インストールする
  2. Pathを通す
  3. Selenium設置フォルダ作る
  4. composerを使用してfacebook-webdriverをインストールする
  5. "selenium-server-standalone-..*.jar"をSelenium設置フォルダへ移動する
  6. "chromedriver.exe"をSelenium設置フォルダへ移動する
  7. seleniumを起動する
  8. phpで書いた自動操作プログラムを実行する。
  9. エラーがあれば解消する

Seleniumフォルダ作る

フォルダ名は例では「Selenium」とする
image.png

composerを使用してfacebook-webdriverをインストールする

下記コマンドを実行する

composer require facebook/webdriver

成功すると下記の表示となる
image.png

Seleniumフォルダに各ファイルを移動

・「chromedriver.exe」を移動
・「selenium-server-standalone-..*.jar」を移動
・demo.phpを作成する
下記phpファイルを設置する(参考元とほぼ同じです。)

demo.php
<?php

require_once './vendor/autoload.php';

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\WebDriverBy;

/**
 * selenium facebook-webdriver 実行のサンプル
 */
function demo()
{
    output(__LINE__,"selenium host",__METHOD__);
    // selenium
    $host = 'http://localhost:4444/wd/hub';
    output(__LINE__,"chromeDriver start",__METHOD__);
    // chrome ドライバーの起動
    $driver = RemoteWebDriver::create($host,DesiredCapabilities::chrome());
    output(__LINE__,"chromeDriver start",__METHOD__);
    // 指定URLへ遷移 (Google)
    $driver->get('https://www.google.co.jp/');
    // 検索Box
    $element = $driver->findElement(WebDriverBy::name('q'));
    // 検索Boxにキーワードを入力して
    $element->sendKeys('セレニウムで自動操作');
    // 検索実行
    $element->submit();
    output(__LINE__,"Serch Start",__METHOD__);

    // 検索結果画面のタイトルが 'セレニウムで自動操作 - Google 検索' になるまで10秒間待機する
    // 指定したタイトルにならずに10秒以上経ったら
    // 'Facebook\WebDriver\Exception\TimeOutException' がthrowされる
    $driver->wait(10)->until(
        WebDriverExpectedCondition::titleIs('セレニウムで自動操作 - Google 検索')
    );

    // セレニウムで自動操作 - Google 検索 というタイトルを取得できることを確認する
    if ($driver->getTitle() !== 'セレニウムで自動操作 - Google 検索') {
        throw new Exception('fail');
    }

    // キャプチャ
    $file = __DIR__ . '/' . __METHOD__ . "_chrome.png";
    $driver->takeScreenshot($file);

    // ブラウザを閉じる
    $driver->close();
}
/**
 * デバッグ出力メソッド
 */
function output($line,$msg="",$method=""){
    echo sprintf("%s:%s:%s",$line,$msg,$method) . PHP_EOL;
}
echo "==== Selenium Start ====" . PHP_EOL;
// 実行
demo();
echo "==== Selenium End ====" . PHP_EOL;

設置後は下記のようになる。
image.png

Seleniumを起動する

java -jar selenium-server-standalone-*.*.*.jar

自動操作を実行する

php demo.php

以上。

エラー対応(Exeption)

Unable to create new service: ChromeDriverService

Fatal error: Uncaught exception 'Facebook\WebDriver\Exception\SessionNotCreatedException' with message 'Unable to create new service: ChromeDriverService
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: '省略

■ 原因
実行ファイルと同じフォルダに「ChromeDiriverService」実行ファイルがない為発生する。

■ 対策
実行ファイルと同フォルダに「ChromeDiriverService.exe」を設置する
参考:https://stackoverflow.com/questions/49845394/selenium-facebook-php-webdriver-unable-to-create-new-service-chromedriverser

session not created: This version of ChromeDriver only supports Chrome version

Fatal error: Uncaught exception 'Facebook\WebDriver\Exception\SessionNotCreatedException' with message 'session not created: This version of ChromeDriver only supports Chrome version 90Current browser version is 89.0.4389.128 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe  (Driver info: chromedriver=90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429}),platform=Windows NT 6.1.7601 SP1 x86) (WARNING: The server did not provide any stacktrace information)Command duration or timeout: 22.11 seconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: '以下省略

■ 原因
エラーメッセージのとおり「ChromeDriver 」がサポートしていない「Chromeブラウザ」を使用しているエラー
■ 対策
Chromeブラウザを最新にアップデートする。

本記事の参考元

Windows用にエラー対応とコマンドを一部変えた以外はほぼ同じです。
https://qiita.com/Rasukarusan/items/0ca204d5b0f0fb876252

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0