Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Selenium use a specific Firefox profile without making a copy

I'm using selenium in python under Linux and have it setup to use a specific Firefox profile. That part is working fine. However, it is creating a copy of the profile in /tmp and not using the profile directory directly in the location I specify ('~/.mozilla/firefox/ki1relie.testprof') with webdriver.FirefoxProfile. Is there an option to tell selenium that I want to use the original profile directory without copying it?

like image 849
deltaray Avatar asked Mar 12 '18 12:03

deltaray


People also ask

What is the basic use of Firefox profiles and how can we use them using Selenium?

Firefox profile is the collection of settings, customization, add-ons and other personalization settings that can be done on the Firefox Browser. You can customize Firefox profile to suit your Selenium automation requirement. Also, Firefox or any other browser handles the SSL certificates settings.

How do I use Firefox options in Selenium?

FirefoxOptions options = new FirefoxOptions(); driver = new RemoteWebDriver(new URL("http://10.x.x.x:4444/wd/hub"), options); When you start your Selenium Nodes, it displays a log information on using new FirefoxOptions preferred to 'DesiredCapabilities. firefox() along with all other browser options.

How do I hide Selenium in Firefox?

We can hide the Firefox window in Selenium webdriver. This can be done by making the browser headless. We shall achieve this with the FirefoxOptions class. We shall then create an object option of that class.


1 Answers

Good reading on the problem is the constructor of firefox_profile.FirefoxProfile() code. It directly states that

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.
"""

in OOP you can subclass it, if you want. like this:

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
like image 106
Mechanic Avatar answered Oct 11 '22 20:10

Mechanic