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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
FredMaster
  • 1,211
  • 1
  • 15
  • 35
  • Have you tried `driver = webdriver.Chrome(chrome_options=chrome_options)` ? It works for me. – HenryM Mar 07 '17 at 11:47
  • Thanks for your reply. Yes, this is what I am doing in the last line of the code...isn't it? – FredMaster Mar 07 '17 at 12:40
  • No, you're including a path. You're also importing path and then overwriting it. – HenryM Mar 07 '17 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` – FredMaster Mar 07 '17 at 12:55
  • Have you installed the webdriver for Chrome? You can find it here. https://sites.google.com/a/chromium.org/chromedriver/downloads – HenryM Mar 07 '17 at 13:12
  • Yes, I have installed it and refer to it using path. Webdriver works just fine. Only adding the extension doesn't work because apparently I am pointing to the wrong directory / path. But not able to find the correct path. Already tried to copy the extension folder to the same folder where webdriver is placed. This also doesn't work....any other idea? – FredMaster Mar 07 '17 at 13:36
  • Does this help?: http://stackoverflow.com/questions/30141847/how-to-select-chrome-extensions-to-enable-when-using-selenium?noredirect=1&lq=1 – HenryM Mar 07 '17 at 14:37
  • I tried exactly this before and it doesn't work. But thanks anyways. – FredMaster Mar 07 '17 at 14:58

1 Answers1

2

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.

crookedleaf
  • 2,118
  • 4
  • 16
  • 38
  • Thanks, crookedleaf. It is working now. Regarding `path`....could have seen this myself. Sometimes I just don't see the obvious when debugging ;-) – FredMaster Mar 08 '17 at 06:58