Skip to content

Instantly share code, notes, and snippets.

@kurozumi

kurozumi/profile.py

Last active Jan 5, 2018
Embed
What would you like to do?
【Python】install.rdfがないアドオンをSeleniumのFirefoxにインストールする方法
import json
import os
import sys
from selenium import webdriver
class FirefoxProfile(webdriver.FirefoxProfile):
"""
FirefoxProfileの拡張クラス
"""
def _addon_details(self, addon_path):
"""
install.rdfがない場合、manifest.jsonを探す
"""
try:
return super()._addon_details(addon_path)
except webdriver.firefox.firefox_profile.AddonFormatError:
try:
with open(os.path.join(addon_path, 'manifest.json'), 'r', encoding='utf-8_sig') as f:
manifest = json.load(f)
return {
'id': manifest['applications']['gecko']['id'],
'version': manifest['version'],
'name': manifest['name'],
'unpack': False,
}
except (IOError, KeyError) as e:
raise AddonFormatError(str(e), sys.exc_info()[2])
from selenium import webdriver
import profile
addon = "extension.xpi"
profile = profile.MyFirefoxProfile()
profile.add_extension(extension=addon)
browser = webdriver.Firefox(profile)
browser.get("https://www.google.co.jp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment