使用Google Chrome Selenium驱动程序的扩展程序

时间:2014-03-02 17:12:50

标签: python google-chrome selenium selenium-webdriver

我正在尝试添加我的扩展程序:

from selenium.webdriver.chrome.options import Options
import os
executable_path = "C:/Chrome/chromedriver.exe"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension('C:/Users/Wilson/AppData/Local/Google/Chrome/User Data/Default/Extensions')

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

但收到以下错误:

Traceback (most recent call last):
  File "C:/Users/Wilson/Dropbox/xxx.py", line 77, in <module>
    driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)
  File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 55, in __init__
    desired_capabilities = options.to_capabilities()
  File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\options.py", line 140, in to_capabilities
    chrome_options["extensions"] = self.extensions
  File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\options.py", line 76, in extensions
    file_ = open(ext, 'rb')
PermissionError: [Errno 13] Permission denied: 'C:/Users/Wilson/AppData/Local/Google/Chrome/User Data/Default/Extensions'

不确定我做错了什么。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

    chop = webdriver.ChromeOptions()
    chop.add_argument('load-extension=path/to/extension')

尝试使用您想要的扩展程序,看看它是否仍然给您相同的错误

这是python的语法

答案 1 :(得分:1)

我遇到了同样的问题而无法让add_extension工作。相反,我们可以使用add_argument函数。我的文件夹结构如下:

ProjectRoot
|-- extension
|   |-- manifest.json
    |-- popup.html
|   `-- other extension files etc...
`-- start.py

这是我的start.py

import inspect, os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
EXTENSION_ROOT = os.path.join(PROJECT_ROOT, 'extension')

chrome_options = Options()
chrome_options.add_argument(
    'load-extension={0}'.format(EXTENSION_ROOT)
)

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('http://www.google.co.uk')