Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Downloads and extracts the correct chromedriver binary for the currently installed Chromium/Chrome version.
#!/usr/bin/env bash
set -xeuo pipefail
function get_major_version () {
echo "${1}" | grep -Po '[^0-9]+\K(\d+)' | head -n1
}
function get_version () {
echo "${1}" | grep -Po '[^0-9]+\K([0-9.]+)' | head -n1
}
BIN_DIR="./.cache/chromedriver"
BIN_PATH="${BIN_DIR}/chromedriver"
echo "Deriving chromedriver version from Chromium version."
MAJOR_VERSION="$(get_major_version "$(chromium-browser --version || google-chrome --version)")"
CHROMEDRIVER_VERSION="$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${MAJOR_VERSION}")"
if [[ -f "${BIN_PATH}" ]]; then
CURRENT_VERSION="$(get_version "$($BIN_PATH --version)")"
if [[ "${CURRENT_VERSION}" == "${CHROMEDRIVER_VERSION}" ]]; then
echo "Skipping download. chromedriver ${CHROMEDRIVER_VERSION} already exists."
exit 0
else
echo "Removing chromedriver ${CURRENT_VERSION}."
rm "${BIN_PATH}"
fi
fi
TMP_DIR="$(mktemp -d)"
ZIP_FILE="${TMP_DIR}/chromedriver.zip"
echo "Downloading chromedriver ${CHROMEDRIVER_VERSION}"
wget -q -O "${ZIP_FILE}" "https://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip"
unzip "${ZIP_FILE}" chromedriver
mkdir -p "${BIN_DIR}"
mv chromedriver "${BIN_PATH}"
rm -rf "${TMP_DIR}"
echo 'Done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment