Use Python Selenium with Snap browsers

TL, DR

Selenium is one of the main libraries for browser automation and web scraping. Sometimes it is painful to integrate with browsers that are installed as Snap packages, this guide gives you a few examples for a correct configuration.

What are Snap packages?

Snap packages is the new and preferred way to distribute applications in Ubuntu and many of its derivatives. Right now, by default, several applications will install as Snaps even if the user installs them via apt. Firefox and Chromium are amongst them.

The behavior of a Snap is quite different from a regular application, as there are specific confinements and other measures to contain the impact between applications and the system. This can create quite a few headaches in automatic calls between applications or, as in our topic, automatic control of a browser for testing or web scraping.

Minimal example for Chromium

Below you can find a minimum working example for Python Selenium and Chromium. It assumes you have installed Chromium as a Snap in its default location.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time

opts = Options()
chromium_path = "/snap/chromium/current/usr/lib/chromium-browser/chrome"
opts.binary_location = chromium_path
chromedriver_path = Service(executable_path="/snap/chromium/current/usr/lib/chromium-browser/chromedriver")
driver = webdriver.Chrome(service=chromedriver_path, options=opts)
driver.get("https://www.google.com")

time.sleep(5)
driver.quit()

Few things worth to note:

  • Snaps automatically create new versions of your application every time there is an update. pointing to the current directory helps you to survive updates without the need to change your path.
  • When you install the Chromium Snap, it also automatically installs its chromedriver binary. You don’t need anymore to look for it and install it independently, and you shouldn’t. Stick to the default, as it will be upgraded in sync with your browser.

That’s it, now you can automate your browser even if it is installed as a Snap! I will publish further tutorials on webscraping, you can find them at this link (also reported below).

Related links

  • More posts about scraping link

Do you like our content? Check more of our posts in our blog!