Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active January 5, 2018 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/f8c9ecaf44c7032c9aae5e9a8630a00c to your computer and use it in GitHub Desktop.
Save kurozumi/f8c9ecaf44c7032c9aae5e9a8630a00c to your computer and use it in GitHub Desktop.
【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