无法在python selenium中加载firefox

时间:2015-09-29 03:57:02

标签: python firefox selenium firefox-addon

我希望在运行selenium webdriver时加载Zenmate Firefox Addons 它运行正常,当我运行而不加载任何插件(扩展)但在添加任何插件时有问题。 这是我的代码:

def __init__(self):   

        self.main_catag_driver = []
        self.declare_xpaths()
        self.main_web_url = "Some_website_url"
        self.path_fire_fox_file = "~/.mozilla/firefox/7rl1aw4c.default/extensions/firefox@zenmate.com.xpi"
        self.driver = webdriver.Firefox()
        self.driver.add_extension(extension=self.path_fire_fox_file)
        time.sleep(5)
        self.driver.get(self.main_web_url)

加载栏时出错:

Traceback (most recent call last):
  File "scrap.py", line 73, in <module>
    obj = carandclassic()
  File "scrap.py", line 28, in __init__
    self.driver.add_extension(extension=self.path_fire_fox_file)
AttributeError: 'WebDriver' object has no attribute 'add_extension'

2 个答案:

答案 0 :(得分:1)

add_extension() is a method on FirefoxProfile,而不是WebDriver

profile = webdriver.FirefoxProfile()
profile.add_extension(extension=self.path_fire_fox_file)

self.driver = webdriver.Firefox(profile)

另外,我不确定你是否真的需要time.sleep(5)电话 - 尝试不浪费5秒。

答案 1 :(得分:0)

虽然我同意alecxe,但它仅适用于某些扩展名。我设法用install_addon()成功地(从.xpi文件)安装了所有这些文件,并在已实例化的webdriver.Firefox上进行了调用:

driver = webdriver.Firefox(executable_path='path_to_geckodriver')
# Path to .xpi file needs to be an absolute path. I think it must be a string too.
driver.install_addon(str(Path('extensions/some_extension.xpi').absolute()))

driver.get("about:addons")  # One step closer to see all extensions at glance