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

标签 python selenium selenium-webdriver

我在 Linux 下的 python 中使用 selenium,并将其设置为使用特定的 Firefox 配置文件。那部分工作正常。但是,它正在/tmp 中创建配置文件的副本,而不是直接在我使用 webdriver.FirefoxProfile 指定的位置 ('~/.mozilla/firefox/ki1relie.testprof') 中使用配置文件目录。是否可以选择告诉 selenium 我想使用原始配置文件目录而不复制它?

最佳答案

关于这个问题的好读物是 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 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 中,您可以根据需要对其进行子类化。像这样:
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

关于python - Selenium 可以在不复制的情况下使用特定的 Firefox 配置文件吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49234989/

相关文章:

使用 Selenium 的 Django 测试未加载固定装置

ruby - 获取测试自动化屏幕截图时的字符编码问题(损坏的字符)

python - 使用 python 更改键盘布局?

java - 如何更改页面源代码?使用 Selenium 和java

java - 使用 selenium webdriver 查找元素的 sibling

python-3.x - 'WebDriverWait(driver, 20)' 是什么意思?

selenium - 为什么使用内置的 webdriver 等待会产生错误 "ReferenceError: until is not defined"?

python - Pandas 如何聚合多个列

python - 如何制作 Matplotlib 动画 fiddle 图?

python - Pandas-合并两列(一列是列表,一列是字符串)