Skip to content

Instantly share code, notes, and snippets.

@ix4
Created February 22, 2024 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ix4/810ffea95a08482672b92d1df3274628 to your computer and use it in GitHub Desktop.
Save ix4/810ffea95a08482672b92d1df3274628 to your computer and use it in GitHub Desktop.
Updates (or installs) ChromeDriver to ensure its version matches the installed Google Chrome version
#!/bin/bash
JSON_URL="https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
# Check if the system is Debian-based
if ! command -v lsb_release &> /dev/null || [ "$(lsb_release -is)" != "Debian" -a "$(lsb_release -is)" != "Ubuntu" ]; then
echo "This script is for Debian-based systems only. ❌"
exit 1
fi
# Function to install Google Chrome
install_google_chrome() {
echo "Google Chrome is not installed. Installing now... πŸ”½"
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list
sudo apt-get update
sudo apt-get install google-chrome-stable
}
# Function to install or update ChromeDriver
install_or_update_chromedriver() {
JSON_DATA=$(curl -s "$JSON_URL")
CHROMEDRIVER_URL=$(echo "$JSON_DATA" | jq -r --arg version "$CHROME_VERSION" '.versions[] | select(.version | startswith($version)) | .downloads.chromedriver[] | select(.platform == "linux64") | .url' | head -n 1)
wget "$CHROMEDRIVER_URL" -O /tmp/chromedriver_linux64.zip
unzip -o /tmp/chromedriver_linux64.zip -d /tmp
sudo mv /tmp/chromedriver-linux64/chromedriver /usr/local/bin/
sudo chmod +x /usr/local/bin/chromedriver
sudo rm -r /tmp/chromedriver_linux64.zip
}
# Check if Google Chrome is installed
if ! dpkg -l | grep google-chrome-stable &> /dev/null; then
install_google_chrome
fi
# Initial version check
CHROME_VERSION=$(google-chrome --version | grep -oE "[0-9]+\\.[0-9]+\\.[0-9]+")
CHROMEDRIVER_VERSION=$(chromedriver --version 2> /dev/null | grep -oE "[0-9]+\\.[0-9]+\\.[0-9]+")
if [ "$CHROME_VERSION" != "$CHROMEDRIVER_VERSION" ] || [ -z "$CHROMEDRIVER_VERSION" ]; then
echo "Chrome version: $CHROME_VERSION 🌐 ChromeDriver version: $CHROMEDRIVER_VERSION πŸš— Mismatch detected, updating... πŸ”Ό"
install_or_update_chromedriver
# Re-check ChromeDriver version after installation/update
NEW_CHROMEDRIVER_VERSION=$(chromedriver --version 2> /dev/null | grep -oE "[0-9]+\\.[0-9]+\\.[0-9]+")
if [ "$CHROME_VERSION" == "$NEW_CHROMEDRIVER_VERSION" ]; then
echo "Update successful! Chrome version: $CHROME_VERSION 🌐 now matches ChromeDriver version: $NEW_CHROMEDRIVER_VERSION πŸš— βœ…"
else
echo "Update attempt completed, but versions might still not match. Please verify manually. ❗"
fi
else
echo "Chrome version: $CHROME_VERSION 🌐 matches ChromeDriver version: $CHROMEDRIVER_VERSION πŸš— All set! βœ…"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment