17

I went to Chrome Extension Downloader to snag the .crx file for 'Adblock-Plus_v1.4.1'.

I threw it in the directory I'm working in, and then ran:

from selenium import webdriver

chop = webdriver.ChromeOptions()
chop.add_extension('Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options = chop)

It totally acknowledges that it exists, but it gives me what looks like a ChromeDriver.exe style message:

ERROR:extension_error_reporter.cc(56)] Extension error: Package is invalid: 'CRX_PUBLIC_KEY_INVALID'.

Then eventually a webdriver exception:

selenium.common.exceptions.WebDriverException: Message: u'Extension could not be installed'

I am almost 100% sure that there is nothing wrong with my code, because of the fact it puts a ChromeDriver type message first before throwing the exception.

I also tried to pack it myself by going to 'C:\Documents and Settings\\*UserName*\Local Settings\Application Data\Google\Chrome\User Data\Default\Extensions' on chrome://extensions/ with developer mode on, tried to use that .crx that was created and got the exact same error message

I also tried a different way:

chop = webdriver.ChromeOptions()
chop.add_argument('--load_extension=Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options = chop)

this doesn't cause an exception or even a Chrome Driver error, but if I manually go to chrome://extensions/ it doesn't say that the extension is loaded...

I'm thinking my problem has to do with the actual .crx file itself. because of the nature of the error message... but then at the same time, I'm not sure because if I spawn a webdriver.Chrome() session, and then manually go to chrome://extensions/ i can physically drag and drop install the same .crx file.

Edit: I realized I didn't actually ask a question so here it is:

What am I doing wrong? Why can't I load this chrome extension? Is it my code, or the .crx file itself?

UPDATE: @Pat Meeker I've tried this, but im losing something in the translation from java to python

capability = webdriver.DesiredCapabilities.CHROME returns a dictionary that has all my arguments in i, so I'm pretty sure the only part that I need to do is add the arguments.

options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/USER_NAME/AppData/Local/Google/Chrome/User Data/Default/')

This is what I have right now, and whenever I try to driver = webdriver.Chrome(chrome_options=options) chrome opens up, and it seems to remember its previous position, but NOTHING more, no bookmarks, no extensions no nothing.

TehTris
  • 3,139
  • 1
  • 21
  • 33
  • 1
    Are you wanting to test the process of actually adding the extension? Or do you just need the extension? If the latter, you should be able to manually add the extension (one time) using a particular chrome user profile, and then in your test - set up your WebDriver using that same profile, and the extension should be there. Here's an answer on setting up the WebDriver with a profile: http://stackoverflow.com/a/15270384/2386700 – Dingredient Jun 11 '13 at 16:40

2 Answers2

21

Just add this extra line in your program

from selenium.webdriver.chrome.options import Options it will work...

like this

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chop = webdriver.ChromeOptions()
chop.add_extension('Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options = chop)
Jingjie Yang
  • 605
  • 2
  • 10
  • 22
arun
  • 1,728
  • 15
  • 15
0

From my skimpy experience the problem is with the load-extesion argument and not your code as I had the same problem with testing an extension that's not from Chrome Web Store. I managed to solve it by installing the extension with Drag & Drop and using only the --user-data-dir argument.

This worked for me with C# and Chrome 33, I know it sounds flimsy but it works for me for several months now so I hope it'll help.

Moshisho
  • 2,781
  • 1
  • 23
  • 39