Using Extensions with Selenium (Python)


I am currently using Selenium to run instances of Chrome to test web pages. Each time my script runs, a clean instance of Chrome starts up (clean of extensions, bookmarks, browsing history, etc). I was wondering if it's possible to run my script with Chrome extensions. I've tried searching for a Python example, but nothing came up when I googled this.

8/5/2014 5:15:55 AM

Accepted Answer

You should use Chrome WebDriver options to set a list of extensions to load. Here's an example:

import os
from selenium import webdriver
from import Options

executable_path = "path_to_webdriver"
os.environ[""] = executable_path

chrome_options = Options()

driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)

Hope that helps.

5/7/2019 8:32:01 PM

The leading answer didn't work for me because I didn't realize you had to point the webdriver options toward a .zip file.

I.e. chrome_options.add_extension('path_to_extension_dir') doesn't work.
You need: chrome_options.add_extension('')

After figuring that out and reading a couple posts on how to create the zip file via the command line and load it into selenium, the only way it worked for me was to zip my extension files within the same python script. This actually turned out to be a nice way for automatically updating any changes you might have made to your extension:

import os, zipfile
from selenium import webdriver

# Configure filepaths
chrome_exe = "path/to/chromedriver.exe"
ext_dir = 'extension'
ext_file = ''

# Create zipped extension
## Read in your extension files
file_names = os.listdir(ext_dir)
file_dict = {}
for fn in file_names:
    with open(os.path.join(ext_dir, fn), 'r') as infile:
        file_dict[fn] =

## Save files to zipped archive
with zipfile.ZipFile(ext_file), 'w') as zf:
    for fn, content in file_dict.iteritems():
        zf.writestr(fn, content)

# Add extension
chrome_options = webdriver.ChromeOptions()

# Start driver
driver = webdriver.Chrome(executable_path=chrome_exe, chrome_options=chrome_options)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow