LoginSignup
11

More than 3 years have passed since last update.

posted at

updated at

Selenium php-webdriverでよく使うChromeオプション

php-webdriverを使ってChrome操作する時によく使うオプションメモ

selenium.php
$options = new ChromeOptions();

//ヘッドレスモードを有効にする
$options->addArguments( [ '--headless' ] );

//シークレットモードを有効にする
$options->addArguments( [ '--incognito' ] );

//プロキシサーバーを設定する
$proxy = "xxx.xxx.xxx.xxx:xxxx";
$options->addArguments( [ '--proxy-server='.$proxy] );

//ブラウザデータ保存先を任意指定する
$tmp = "/tmp/chrome/";
$options->addArguments( [ 'user-data-dir='.$tmp]);

//言語を指定する(日本語)
$options->addArguments( [ '--lang=ja-JP' ] );

//UserAgentを指定する
$UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36";
$options->addArguments( [ '--user-agent=' . $UserAgent ] );

//起動時の画面幅を指定する
$width  = 1280;
$height = 1024;
$options->addArguments( [ "window-size={$width},{$height}" ] );

$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability( ChromeOptions::CAPABILITY, $options );
try{
   $driver = RemoteWebDriver::create( $this->selenium_server, $capabilities );
〜〜以下略〜〜

//ヘッドレスモードでファイルをダウンロードする場合の問題を回避
//クラスを継承しておく必要があります extends Facebook\WebDriver\Remote\HttpCommandExecutor
$commands = parent::$commands;
$commands['chromiumSendCommand'] = [ 'method' => 'POST', 'url' => '/session/:sessionId/chromium/send_command' ];
self::$commands = $commands;
//DL場所を指定する
$dl_path = "/tmp"
$driver->execute('chromiumSendCommand', [
   'cmd'    => 'Page.setDownloadBehavior',
   'params' => [ 'behavior' => 'allow', 'downloadPath' => $dl_path ],
]);


参考リンク
php-webdriver

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
What you can do with signing up
11