首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium可以在不复制的情况下使用特定的Firefox配置文件吗

Selenium可以在不复制的情况下使用特定的Firefox配置文件吗
EN

Stack Overflow用户
提问于 2018-03-12 20:13:26
回答 1查看 787关注 0票数 6

我在Linux下的python中使用selenium,并将其设置为使用特定的Firefox配置文件。这部分运行得很好。但是,它在/tmp中创建了配置文件的副本,而不是直接在我使用webdriver.FirefoxProfile指定的位置('~/.mozilla/firefox/ki1relie.testprof')使用配置文件目录。有没有一个选项可以告诉selenium我想使用原始的配置文件目录而不复制它?

EN

回答 1

Stack Overflow用户

发布于 2020-12-09 21:55:24

这个问题的好读物是firefox_profile.FirefoxProfile()代码的构造器。它直接说明了

代码语言:javascript
复制
def __init__(self, profile_directory=None):
    """
    Initialises a new instance of a Firefox Profile
:args:
 - profile_directory: Directory of profile that you want to use. If a
   directory is passed in it will be cloned and the cloned directory
   will be used by the driver when instantiated.
   This defaults to None and will create a new
   directory when object is created.
"""

在OOP中,如果你愿意,你可以子类化它。如下所示:

代码语言:javascript
复制
import copy
import json
import os

from selenium import webdriver
from selenium.webdriver.firefox import firefox_profile
    
class MyVeryFirefoxProfile(firefox_profile.FirefoxProfile):

    def __init__(self, profile_directory=None):
        """
        Initialises a new instance of a Firefox Profile

        :args:
         - profile_directory: Directory of profile that you want to use.
           If a directory is passed it will be used as is, with no cloning attempts.
           Otherwise it will be handled as usual (new empty profile dir on user's temp)
        """
        if (profile_directory == None):
            # just pass it to super
            super().__init__(profile_directory)
            return
                    
        if not firefox_profile.FirefoxProfile.DEFAULT_PREFERENCES:
            with open(os.path.join(os.path.dirname(firefox_profile.__file__),
                                   firefox_profile.WEBDRIVER_PREFERENCES)) as default_prefs:
                firefox_profile.FirefoxProfile.DEFAULT_PREFERENCES = json.load(default_prefs)

        self.default_preferences = copy.deepcopy(
            firefox_profile.FirefoxProfile.DEFAULT_PREFERENCES['mutable'])
        self.profile_dir = profile_directory
        self.tempfolder = None # if smart enough, it will understand :-)
        #===================================================================
        # newprof = os.path.join(self.tempfolder, "webdriver-py-profilecopy")
        # shutil.copytree(self.profile_dir, newprof,
        #                 ignore=shutil.ignore_patterns("parent.lock", "lock", ".parentlock"))
        # self.profile_dir = newprof
        # os.chmod(self.profile_dir, 0o755)
        #===================================================================
        self._read_existing_userjs(os.path.join(self.profile_dir, "user.js"))
        self.extensionsDir = os.path.join(self.profile_dir, "extensions")
        self.userPrefs = os.path.join(self.profile_dir, "user.js")
        if os.path.isfile(self.userPrefs):
            os.chmod(self.userPrefs, 0o644)

def main():
    profile = MyVeryFirefoxProfile(os.path.abspath(FIREFOX_PROFILE_PATH))
    driver = webdriver.Firefox(firefox_profile=profile)
    .. blah ..
    # and no more bloating temp folders ;-)
    
    # but don't forget to fool this killer just before calling quit()
    driver.profile = None
    driver.quit()

    # otherwise your profile will be killed from disk
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49234989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档