Google collab is using Ubuntu 20.04 now and you can't install chromium-browser without snap. But you can install it by using .deb files for ubuntu 18.04 at security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/.
I made a python script for this purpose. It finds latest version of chromium-browser and chromedriver for 18.04 and installs it for your google colab which has Ubuntu 20.04.
Site's links have been updated regularly. You don't need debian repository and apt keys.
import os
import re
import subprocess
import requests
# The deb files we need to install
deb_files_startstwith = [
"chromium-codecs-ffmpeg-extra_",
"chromium-codecs-ffmpeg_",
"chromium-browser_",
"chromium-chromedriver_"
]
def get_latest_version() -> str:
# A request to security.ubuntu.com for getting latest version of chromium-browser
# e.g. "112.0.5615.49-0ubuntu0.18.04.1_amd64.deb"
url = "http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/"
r = requests.get(url)
if r.status_code != 200:
raise Exception("status_code code not 200!")
text = r.text
# Find latest version
pattern = '<a\shref="chromium\-browser_([^"]+.ubuntu0\.18\.04\.1_amd64\.deb)'
latest_version_search = re.search(pattern, text)
if latest_version_search:
latest_version = latest_version_search.group(1)
else:
raise Exception("Can not find latest version!")
return latest_version
def download(latest_version: str, quiet: bool):
deb_files = []
for deb_file in deb_files_startstwith:
deb_files.append(deb_file + latest_version)
for deb_file in deb_files:
url = f"http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/{deb_file}"
# Download deb file
if quiet:
command = f"wget -q -O /content/{deb_file} {url}"
else:
command = f"wget -O /content/{deb_file} {url}"
print(f"Downloading: {deb_file}")
# os.system(command)
!$command
# Install deb file
if quiet:
command = f"apt-get install /content/{deb_file} >> apt.log"
else:
command = f"apt-get install /content/{deb_file}"
print(f"Installing: {deb_file}\n")
# os.system(command)
!$command
# Delete deb file from disk
os.remove(f"/content/{deb_file}")
def check_chromium_installation():
try:
subprocess.call(["chromium-browser"])
print("Chromium installation successfull.")
except FileNotFoundError:
print("Chromium Installation Failed!")
def install_selenium_package(quiet: bool):
if quiet:
!pip install selenium -qq >> pip.log
else:
!pip install selenium
def main(quiet: bool):
# Get the latest version of chromium-browser for ubuntu 18.04
latest_version = get_latest_version()
# Download and install chromium-browser for ubuntu 20.04
download(latest_version, quiet)
# Check if installation succesfull
check_chromium_installation()
# Finally install selenium package
install_selenium_package(quiet)
if __name__ == '__main__':
quiet = True # verboseness of wget and apt
main(quiet)
And try selenium
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
wd = webdriver.Chrome('chromedriver', options=chrome_options)
wd.get("https://www.google.com")
print(f"Page title: {wd.title}")