Selenium
主なオプションまとめ
| Python | About |
ヘッドレス | –headless | ブラウザを立ち上げず実行する |
ミュート | –mute-audio | ミュートにする |
サンドボックス | –no-sandbox | サンドボックスを使用しない |
/dev/shm | –disable-dev-shm-usage | /dev/shm を使用しない |
| –disable-gpu | とりあえず付けとけとか |
| –disable-web-security | |
| –user-data-dir | |
| –allow-running-insecure-content | |
サンドボックス
サンドボックスとは、外部から受け取ったプログラムを保護された領域で動作させることによって、システムが不正に操作されるのを防ぐセキュリティ機構のこと
Chrome(Chromium)ではセキュリティ上の理由で、レンダリングやスクリプトエンジンはSandboxというchrootで隔離された環境で動かされる
そのため、Chromeの自動操作にはサンドボックスを無効化しなければならない
/dev/shm
Exclude the collection
option.add_experimental_option('excludeSwitches', ['enable-logging', 'enable-automation'])
オプション | 概要 |
enable-logging | コマンドラインにログが出ない |
enable-automation | 自動なんちゃらによって保護されています的なやつ無効 |
ウィンドウ最大化
スクリーンショット
driver.save_screenshot('test.png')
フルページ
w = driver.execute_script('return document.body.scrollWidth')
h = driver.execute_script('return document.body.scrollHeight')
driver.set_window_size(w, h)
Tips
Googleにログイン
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
参照
Seleniumドキュメント
Seleniumブラウザー自動化プロジェクト
-
2020/11/20
List of Chromium Command Line Switches
Peter Beverloo
-
2021/05/26
SeleniumでDevToolsActivePort file doesn't exist
Webアプリ組み込み目的でSelenium WebDriver + Headless Chromium/Firefoxを使うときの雑多な知見