Using Extensions with Selenium (Python)
Asked Answered
S

8

29

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.

Shingle answered 12/5, 2013 at 19:49 Comment(0)
G
37

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.

Golding answered 12/5, 2013 at 21:2 Comment(3)
Hi, I tested your code. But things may seem deprecated. I am getting a traceback about the directory of the extension IsADirectoryError [Error 21]. I just want to know how to write the full path to the extension. What I have here is path/to/XXXXXXXXXXXXXXXXX/1.2.345 should I put this version number in the path??Redden
I hope you help. I have used this answer to get the path to the extension so do you have an Idea why I am facing this error? IsADirectoryError [Error 21]. Thanks in advance.Redden
One question: is there a setting that allows you to block "thanks for installing..." tabs?Hawaii
S
15

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()
Stendhal answered 25/7, 2018 at 22:3 Comment(3)
This worked for me after countless hours. Thank you.Fussell
Unfortunately it is not working for me, can you provide an example of the values that need to go into ext_dir = 'extension' ext_file = 'extension.zip'Etherize
ext_dir should be a directory containing the chrome extension you want to load. The script I posted zips that directory and creates the ext_file, which you then add to the browser options. The simplest extension folders consist of only a mainfest.json and a background.js script. For more info about extensions generally you can check out this getting started guide (developer.chrome.com/extensions/getstarted) and this example (github.com/GoogleChrome/chrome-app-samples/tree/master/samples/…).Stendhal
U
7

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,

Chrome screenshot

  1. Click on your extension 'details'. In my Chrome version, it was on right top click (3 dots) -> 'More tools' -> 'Extensions'.
  2. Have the developer mode enabled in your chrome
  3. Click 'Pack extension' (As shown above) and pack it.
  4. 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

Unbalanced answered 21/7, 2020 at 11:49 Comment(0)
I
4

An alternative way is to use the unpacked folder method. I found the crx and zip method did not work at all on Selenium Grid. In this method, you need to copy the extension from the user data in a Chrome version where you have already manually installed it, the long ID made up of loads of letters like pehaalcefcjfccdpbckoablngfkfgfgj, to the user data directory of the Selenium-controlled Chrome (which you can choose at runtime using the first line of this code, and it will get populated automatically). It should be in the same equivalent directory (which is Extensions). The path must take you all the way to the directory where there is a manifest.json, hence in this example '1.1.0'

chrome_options.add_argument("user-data-dir=C:/Users/charl/OneDrive/python/userprofile/profilename"
unpacked_extension_path = 'C:/Users/charl/OneDrive/python/userprofile/profilename/Default/Extensions/pehaalcefcjfccdpbckoablngfkfgfgj/1.1_0'
chrome_options.add_argument('--load-extension={}'.format(unpacked_extension_path))
driver = webdriver.Chrome(options=chrome_options)   
Impregnate answered 16/3, 2021 at 22:3 Comment(1)
this worked for me! well done my friend!Seftton
O
3

If you wanna import any chrome extension in your selenium python scrip

  1. Put your extension.crx.crx file in the same folder as your code or give the path

  2. 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

Oleograph answered 20/12, 2019 at 15:18 Comment(0)
L
1

I also needed to add an extention to chrome while using selenium. What I did was first open the browser using selenium then add extention to the browser in the normal way like you would do in google chrome.

Legman answered 23/5, 2020 at 11:8 Comment(1)
This works if you are okay with some manual steps. It's non-ideal for those trying to fully automate whatever it is they are doing in selenium.Shingle
R
1

I've found the simpliest way ever.

So no ways were working for me, I tryed to make a crx file and more but nothing worked. So I simply added the unpacked extension path like that:

options.add_argument('--load-extension={}'.format("HERE YOUR EXTENSION PATH"))

Replace the HERE YOUR EXTENSION PATH, by the path of your extension which is in one of your Chrome profiles. To find it use this path on windows: C:\Users[login_name]\AppData\Local\Google\Chrome\User Data\Default\Extensions

Then you have to choose the folder of the extension, it's by ID which you can find in the https://chrome.google.com/webstore/category/extensions, just type there your extension name and in the URL you'll get the ID. Then there in this folder there will be the version of your extensions like: 10.0.3 choose it and it will be your path, so the path must end with the version.

Example:

options.add_argument('--load-extension={}'.format(r'C:\Users\nevo\AppData\Local\Google\Chrome\User Data\Default\Extensions\nkbihfbeogaeaoehlefnkodbefgpgknn\10.20.0_0'))

Note the "r" before the string to make it as raw, or I had to doble the backslashes.

And it works!

Rectifier answered 15/10, 2022 at 15:24 Comment(1)
This works perfectly, but it was relatively harder to find the ID of the extension as you mentioned. I found it by clicking on Manage extension by right-clicking on the extension itself. The ID will be shown in the URL or in the page itself.Footie
Q
0

You can use options to define a Chrome profile and install the Extensions on that profile. This will solve the problem for good, I use it for both chrome settings and extensions.

def init_browser():
  chrome_options = Options()
  chrome_options.add_argument("user-data-dir=Path");


  return Browser("chrome", options=chrome_options, headless=False)

I am using Splinter in this code, however it also works with Selenium.

P.s. If the path does not have a chrome profile, it will generate one.

Qumran answered 16/1 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.