Using Extensions with Selenium (Python)
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('path_to_extension_dir.zip')
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 = 'extension.zip'
# 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] = infile.read()
## 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()
chrome_options.add_extension(ext_file)
# Start driver
driver = webdriver.Chrome(executable_path=chrome_exe, chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()
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 selenium.webdriver.chrome.options import Options
executable_path = "path_to_webdriver"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension('path_to_extension')
driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()
Hope that helps.
If you wanna import any chrome extension in your selenium python scrip
Put your extension.crx.crx file in the same folder as your code or give the path
you can copy-paste this code and just change the file crx.crx name
import os from selenium import webdriver from selenium.webdriver.chrome.options import Options
executable_path = "/webdrivers" os.environ["webdriver.chrome.driver"] = executable_path chrome_options = Options() chrome_options.add_extension(' YOUR - EXTIONTION - NAME ') driver = webdriver.Chrome(chrome_options=chrome_options) driver.get("http://stackoverflow.com")
if this code is throwing an error maybe this will solve it
This is because the selenium expects the packed extensions as the extension argument which will be with .crx
extension.
I already had the extension in my chrome. Below are the steps I followed to pack the existing extension,
- Click on your extension 'details'. In my Chrome version, it was on
right top click (3 dots) -> 'More tools' -> 'Extensions'.
- Have the developer mode enabled in your chrome
- Click 'Pack extension' (As shown above) and pack it.
- This will get stored in the extensions location. For me it was on
/home/user/.config/google-chrome/Default/Extensions/fdjsidgdhskifcclfjowijfwidksdj/2.3.4_22.crx
That's it, you can configure the extension in your selenium as argument.
extension='/home/user/.config/google-chrome/Default/Extensions/fdjsidgdhskifcclfjowijfwidksdj/2.3.4_22.crx'
options = webdriver.ChromeOptions()
options.add_extension(extension)
NOTE: You can also find the 'fdjsidgdhskifcclfjowijfwidksdj' id in the extensions url as query param