Running Selenium WebDriver using Python with exten

2019-01-14 12:53发布

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.

2条回答
beautiful°
2楼-- · 2019-01-14 13:02

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.

查看更多
3楼-- · 2019-01-14 13:10

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)
查看更多
登录 后发表回答