LoginSignup
2
1

More than 3 years have passed since last update.

[chromedriver] Chromeのバージョンを自動取得

Posted at

Chromeのバージョンを自動取得

背景

  • Seleniumでブラウザ操作をするためにchromedriver-binaryを使用したかったが、
    ローカルのGoogleChromeのバージョンによってchromedriver-binaryのバージョンを変える必要があるので
    自動でバージョン判別してインストールできるようにしたのでメモです

スクリプト

  • Windows bat
@rem python activate
python -m venv venv
call "venv\Scripts\activate"

@rem find Chrome Version
FOR /F %%i in ('dir /B /O-N "C:\Program Files (x86)\Google\Chrome\Application" ^| findstr "^[0-9].*\>"') do set VERSION=%%i

echo %VERSION%
@if "%VERSION%" EQU "" (
    echo;
    echo Chrome Version Not Found
    PAUSE
    exit /b
)

@ ↓ マイナーバージョンまで一致すれば動くみたいなのでそれ以外切り捨てしてインストール
pip install chromedriver-binary~=%VERSION:~0,4%
  • bash
#!/usr/bin/env bash

# python activate
python3 -m venv venv
source venv/bin/activate

# find Chrome Version
#  ↓ マイナーバージョンまで一致すれば動くみたいなのでそれ以外切り捨て
version=$(ls /Applications/Google\ Chrome.app/Contents/Frameworks/Google\ Chrome\ Framework.framework/Versions/ | sort | grep -v "Current" | tail -n 1 | sed -E 's/(^[0-9]+.[0-9]+).*/\1/g')

if [ -z "$version" ]; then
  echo "Chrome Version Not Found"
  exit 1
fi

# インストール
pip install chromedriver-binary~="$version"
2
1
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
2
1