3

I need to test Firefox using an extension. I want to automate the test and visit several websites.

I installed Selenium and it opens in geckodriver. However, the extension is not there. I can install it manually from about:debugging but the problem is that I want the Selenium test to launch the gecko driver while the extension is already there. How to do this? How to install the extension permanently in the geckodriver so it is there when I launch the geckodriver from selenium?

EDIT: I also tried to install the extension (add it to the browser) from the Firefox extensions websites. It gets added but once I close the gecko window, the extension disappear in the next run. How to install it permanently?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user9371654
  • 2,160
  • 16
  • 45
  • 78

4 Answers4

5

Note: OP didn't specify a language, so this answer is for Python. The other Selenium WebDriver language bindings have similar mechanisms for creating profiles and adding extensions.


You can install the Extension each time you instantiate the driver.

First, download the extension (XPI file) you want from: https://addons.mozilla.org.

Then, in your code... create a FirefoxProfile() and use the add_extension() method to add the extension. Then you can instantiate a driver using that profile.

For example, this will launch Firefox with a newly created profile containing the "HTTPS Everywhere" extension:

from selenium import webdriver

profile = webdriver.FirefoxProfile() 
profile.add_extension(extension='https_everywhere-2019.1.31-an+fx.xpi')
driver = webdriver.Firefox(firefox_profile=profile) 
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • We are trying to do something like this but inside of Nightwatch.js. https://stackoverflow.com/questions/69565983/how-to-configure-nightwatch-js-with-geckodriver-to-install-metamask-on-browser-s Does anybody here know how this is possible? – glo Oct 14 '21 at 06:34
4

You need to launch geckdriver with an exisitng profile by specifying the profile path of firefox

For python you can do it by this:

profile = FirefoxProfile('/home/student/.mozilla/firefox/gwi6uqpe.Default') // change this path
browser = webdriver.Firefox(firefox_profile=profile)

For C# you can do this:

string path = @"C:\Users\username\AppData\Local\Mozilla\Firefox\Profiles\myi5go1k.default";
FirefoxProfile ffprofile = new FirefoxProfile(path);
Driver = new FirefoxDriver(ffprofile);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Oussail
  • 2,200
  • 11
  • 24
  • Thanks. Can you give me step by step? I have the extension installed in Firefox Developer Edition. The Firefox developer edition is in this path in my device: `/home/xx/Documents/ff_extension/firefox/firefox.exe`. where can I find the path to the default profile for the Firefox Developer Edition? I do not find anything. – user9371654 Feb 18 '19 at 21:06
  • I tested it. i get this error: `NameError: name 'FirefoxProfile' is not defined`. I also tried to add this line but did not solve the problem: `from selenium.webdriver.firefox.firefox_binary import FirefoxProfile` – user9371654 Feb 18 '19 at 21:13
  • I really don't know where does Developer edition keeps it's profiles – Oussail Feb 18 '19 at 21:28
  • Look at this [link](https://sqa.stackexchange.com/questions/2811/how-to-specify-a-firefox-profile-name-when-using-webdriver-python) – Oussail Feb 18 '19 at 21:31
3

I found that this was warking for me:

from selenium import webdriver

driver_path = r"G:\Libs\geckoDriver\firefox\geckodriver.exe"
driver = webdriver.Firefox(executable_path=driver_path)

path = r"G:\Libs\ext\uBlock0_1.38.7b5.firefox.signed.xpi"
driver.install_addon(path, temporary=True)

driver.profile = webdriver.FirefoxProfile()
driver.profile.add_extension(path)
driver.profile.set_preference("security.fileuri.strict_origin_policy", False)
driver.profile.update_preferences()`enter code here`

Reference:

[Python] https://cyruslab.net/2020/08/26/python-adding-extension-to-geckodriver-with-selenium/

Kuba Jurek
  • 61
  • 3
2

You can install an extension/addon permanently within a specific Firefox Profile and use it. To achieve that you need follow the below mentioned steps:

  • You need to create a new Firefox Profile manually (e.g. FirefoxExtensionProfile) following the instructions at Creating a new Firefox profile on Windows.

  • Open a Firefox Browsing session manually and invoke the url https://addons.mozilla.org/en-US/firefox/

  • In the Search Box search for an extension e.g. HTTPS Everywhere.

  • Click on the search result and install / enable (incase previously installed and currently disabled) the extension.

  • Now you can use the following Java solution to open the Firefox Profile FirefoxExtensionProfile containing the extension HTTPS Everywhere

  • Code Block:

    package A_MozillaFirefox;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.firefox.ProfilesIni;
    
    public class A_FirefoxProfile_dc_opt {
    
     public static void main(String[] args) {
    
         System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
         ProfilesIni profile = new ProfilesIni();
         FirefoxProfile testprofile = profile.getProfile("FirefoxExtensionProfile");
         FirefoxOptions opt = new FirefoxOptions();
         opt.setProfile(testprofile);
         WebDriver driver =  new FirefoxDriver(opt);
         driver.get("https://www.google.com");
     }
    }
    
  • Browser Snapshot:

Extension_HTTPS Everywhere


Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352