0

I am trying to load a google chrome extension with Selenium WebDriver.

But I receive an error OSError: Path to the extension doesn't exist.

Here is the code I am using:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from os import path

path = "path to chrome driver"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_extension('Adblock-Plus_v1.12.4_0.crx') # ALTERNATIVE 0
driver = webdriver.Chrome(path, chrome_options=chrome_options)

After reading various similar questions on this site I tried the following two alternatives:

# Alternative 1
chrome_options.add_extension('~/Library/Application\ Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx')

#Alternative 2
chrome_options.add_extension(path.abspath("Adblock-Plus_v1.12.4_0.crx"))

But none of them work. Alternative 1 gives same error message as the original code whereas Alternative 2 gives error AttributeError: 'str' object has no attribute 'abspath'

Does anyone have a clue what I could be doing differently?

8
  • Have you tried driver = webdriver.Chrome(chrome_options=chrome_options) ? It works for me. Commented Mar 7, 2017 at 11:47
  • Thanks for your reply. Yes, this is what I am doing in the last line of the code...isn't it? Commented Mar 7, 2017 at 12:40
  • No, you're including a path. You're also importing path and then overwriting it. Commented Mar 7, 2017 at 12:42
  • Ok, understand what you mean. Tried this out but I do get the same error message OSError: Path to the extension doesn't exist Commented Mar 7, 2017 at 12:55
  • Have you installed the webdriver for Chrome? You can find it here. sites.google.com/a/chromium.org/chromedriver/downloads Commented Mar 7, 2017 at 13:12

1 Answer 1

3

More thank likely, this is because python is referencing the wrong path with what would normally be the home directory shortcut of ~/ in the path. Python will try and run the file from the current directory, so for example if your code is in ~/Dev/testproject, and the code being called above is really trying to run /home/username/Dev/testproject/~/Library/Application Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx

Try using the following:

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

chromedriver = "path to chrome driver"
chrome_options = webdriver.ChromeOptions()


# choose one of the following 2:
chrome_options.add_extension(os.path.expanduser('~/Library/Application\ Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx'))  # Option 1: if your extension is not also in your project folder
chrome_options.add_extension(os.path.abspath('Adblock-Plus_v1.12.4_0.crx'))  # Option 2: if your extension IS in your project folder


driver = webdriver.Chrome(chromedriver, chrome_options=chrome_options)

EDIT: avoid declaring a variable named path since you are importing path from os. This is why you are getting the error on Alternative #2.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, crookedleaf. It is working now. Regarding path....could have seen this myself. Sometimes I just don't see the obvious when debugging ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.