当前位置: 首页>>代码示例>>Python>>正文


Python FirefoxProfile._addon_details方法代码示例

本文整理汇总了Python中selenium.webdriver.firefox.firefox_profile.FirefoxProfile._addon_details方法的典型用法代码示例。如果您正苦于以下问题:Python FirefoxProfile._addon_details方法的具体用法?Python FirefoxProfile._addon_details怎么用?Python FirefoxProfile._addon_details使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在selenium.webdriver.firefox.firefox_profile.FirefoxProfile的用法示例。


在下文中一共展示了FirefoxProfile._addon_details方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _addon_details

# 需要导入模块: from selenium.webdriver.firefox.firefox_profile import FirefoxProfile [as 别名]
# 或者: from selenium.webdriver.firefox.firefox_profile.FirefoxProfile import _addon_details [as 别名]
    def _addon_details(self, addon_path):
        """Selenium 3.4.0 doesn't support loading WebExtensions. See bug:
        https://github.com/SeleniumHQ/selenium/issues/4093. This patch uses
        code from PR: https://github.com/SeleniumHQ/selenium/pull/4790"""
        try:
            return BaseFirefoxProfile._addon_details(self, addon_path)
        except AddonFormatError:
            pass

        # Addon must be a WebExtension, parse details from `manifest.json`
        details = {
            'id': None,
            'unpack': False,
            'name': None,
            'version': None
        }

        def get_namespace_id(doc, url):
            attributes = doc.documentElement.attributes
            namespace = ""
            for i in range(attributes.length):
                if attributes.item(i).value == url:
                    if ":" in attributes.item(i).name:
                        # If the namespace is not the default one remove xlmns:
                        namespace = attributes.item(i).name.split(':')[1] + ":"
                        break
            return namespace

        def get_text(element):
            """Retrieve the text value of a given node"""
            rc = []
            for node in element.childNodes:
                if node.nodeType == node.TEXT_NODE:
                    rc.append(node.data)
            return ''.join(rc).strip()

        if not os.path.exists(addon_path):
            raise IOError('Add-on path does not exist: %s' % addon_path)

        try:
            if zipfile.is_zipfile(addon_path):
                # Bug 944361 - We cannot use 'with' together with zipFile
                # because it will cause an exception thrown in Python 2.6.
                try:
                    compressed_file = zipfile.ZipFile(addon_path, 'r')
                    manifest = compressed_file.read('install.rdf')
                finally:
                    compressed_file.close()
            elif os.path.isdir(addon_path):
                manifest_source = 'manifest.json'
                with open(os.path.join(addon_path, manifest_source), 'r') as f:
                    manifest = f.read()
            else:
                raise IOError("Add-on path is neither an XPI nor a "
                              "directory: %s" % addon_path)
        except (IOError, KeyError) as e:
            raise AddonFormatError(str(e), sys.exc_info()[2])

        doc = json.loads(manifest)

        try:
            details['version'] = doc['version']
            details['name'] = doc['name']
        except KeyError:
            raise AddonFormatError(
                "Add-on manifest.json is missing mandatory fields. "
                "https://developer.mozilla.org/en-US/Add-ons/"
                "WebExtensions/manifest.json")

        try:
            id_ = doc['applications']['gecko']['id']
        except KeyError:
            id_ = "%[email protected]%s" % (doc['name'], doc['version'])
            id_ = ''.join(id_.split())
        finally:
            details["id"] = id_

        return details
开发者ID:gunesacar,项目名称:OpenWPM,代码行数:80,代码来源:selenium_firefox.py


注:本文中的selenium.webdriver.firefox.firefox_profile.FirefoxProfile._addon_details方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。