LoginSignup
6
3

More than 1 year has passed since last update.

Seleniumのヘッドレスモードの設定が変更されます

Posted at

元記事

公式記事のざっくり日本語記載です。

概要

Chromium ベースのブラウザーには、2 つのヘッドレスモードがあります。
古いモードと新しいモードです。

新しいモードは2022年に追加されたもので、詳細については 公式 Chrome ブログ をご覧ください。

通常、Seleniumでは新しいモードのほうがよりよく動きます。

古いモードは順次非推奨/削除します。

新しいヘッドレスモードの指定方法

ブログ内の How can I set headless mode from now on? 内に各言語の例が書かれています。

Ruby の例を以下に書きます。オプション引数に '--headless=new' を渡します。

options = Selenium::WebDriver::Options.chrome(args: ['--headless=new'])
driver = Selenium::WebDriver.for :chrome, options: options
driver.get('https://selenium.dev')
driver.quit

記事内容は以上です。

余談: 使ってみた

公式 Chrome ブログ を見ると、headlessとheadfullのコードを統一したとある。

新規のスクレイピングスクリプト作成で使ってみたところ、headlessでもheadfullでもたしかに動作が変わらないように見えた。

過去は以下のようにオプションでJSの有効化など試行錯誤していたのを削減できたので、その負担が減りそう。

options = Selenium::WebDriver::Options.chrome
# 新しいヘッドレスモード指定
options.add_argument('--headless=new')
# 過去のヘッドレスモード指定
# options.add_argument('--headless')
# options.add_argument("--nogpu")
# options.add_argument('--disable-gpu')
# options.add_argument('--no-sandbox')
# options.add_argument("--enable-javascript")
# window-size はサイトによっては必要そう
# options.add_argument('--window-size=1920,1080')
6
3
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
6
3