September 7, 2018

Python - Using Chrome Extensions With Selenium WebDriver

When you launch a Chrome browser from Selenium, a fresh temporary profile is created for the browser session. This means that by default, Chrome extensions are not loaded. To use an extension during a Selenium test, you must install the extension each time you launch a new browser.


Example 1: Installing a packed extension

A packed extension comes in the form of a single .crx file.

You can install a packed extension like this:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
packed_extension_path = '/path/to/packed/extension.crx'
options = Options()
options.add_extension(packed_extension_path)
driver = webdriver.Chrome(options=options)


Example 2: Installing an unpacked extension

An unpacked extension is a directory containing extension code.

You can install an unpacked extension like this:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
unpacked_extension_path = '/path/to/unpacked/extension/'
options = Options()
options.add_argument('--load-extension={}'.format(unpacked_extension_path))
driver = webdriver.Chrome(options=options)

examples tested with: chromium 68, chromedriver 2.41, selenium 3.14