Last active
January 5, 2018 11:52
-
-
Save kurozumi/f8c9ecaf44c7032c9aae5e9a8630a00c to your computer and use it in GitHub Desktop.
【Python】install.rdfがないアドオンをSeleniumのFirefoxにインストールする方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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