Selenium :: Python :: Chrome扩展

时间:2017-03-07 11:04:29

标签: python google-chrome selenium

我正在尝试使用9加载google chrome extension

但我收到错误Selenium WebDriver

以下是我正在使用的代码:

OSError: Path to the extension doesn't exist

在阅读本网站上的各种类似问题之后,我尝试了以下两种选择:

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)

但它们都不起作用。备选方案1提供与原始代码相同的错误消息,而备选方案2提供错误# 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"))

有没有人知道我的做法可能与众不同?

1 个答案:

答案 0 :(得分:2)

更可能的是,这是因为python引用了错误的路径,通常是路径中foreach的主目录快捷方式。 Python将尝试从当前目录运行该文件,例如,如果您的代码位于~/,并且上面调用的代码确实正在尝试运行~/Dev/testproject

尝试使用以下内容:

/home/username/Dev/testproject/~/Library/Application Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx

编辑:避免声明名为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) 的变量,因为您要从path导入path。这就是您在备选#2上收到错误的原因。