Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Selenium WebDriver using Python with extensions (.crx files)

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.

like image 538
TehTris Avatar asked May 28 '13 20:05

TehTris


People also ask

Can Selenium use extensions?

This can be specified before launching Chrome with Selenium by creating a ChromeOptions instance and calling add_argument() . The above code will setup a Selenium driver for Chrome with the extension located at path/to/extension preinstalled. which includes only the minimum required fields and one content script.

How do I use Python extensions?

To configure an extension, we have to obtain the . crx extension file of the extension. Then we have to perform installation of the extension to the browser which is launched by Selenium. To get all the extensions available to the browser enter chrome://extensions on the browser address bar.


1 Answers

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)
like image 156
arun Avatar answered Sep 22 '22 00:09

arun