Created
February 22, 2024 20:11
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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