I went through a number of posts but none of them could help me get it working.
I would like to install my firefox addon using selenium and Java program i.e. have the addon installed when the firefox is launched.
Note: If I install my addon(*.xpi file) manually to the firefox browser by using opiton "Install Add-on from File.." option in settings, it installs correctly.
Java code:
System.setProperty("webdriver.gecko.driver", "geckodriver-v0.24.0-win64\\geckodriver.exe");
FirefoxOptions firefoxOptions = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File("my_webext.xpi"));
firefoxOptions.setProfile(profile);
WebDriver firefoxDriver = new FirefoxDriver(firefoxOptions);
firefoxDriver.get("http://www.google.com");
Selenium dependency version: (4.0.0-rc-1/4.0.0-alpha-2/3.141.59)
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-2</version>
</dependency>
Gecko Driver version: 0.24.0 or 0.29.1
Firefox version: 92.0(64-bit)
IDE: STS
OS: Windows 10
When I run the above code, the Firefox borwser opens, but the addon is not installed.
I see the following message on console:
1631259829322 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\ab\\AppData\\Local\\Temp\\rust_mozprofile.CSQe58I2OCxk"
1631259830585 Marionette INFO Marionette enabled
JavaScript error: resource://gre/modules/XULStore.jsm, line 66: Error: Can't find profile directory.
console.warn: SearchSettings: "get: No settings file exists, new profile?" (new NotFoundError("Could not open the file at C:\\Users\\ab\\AppData\\Local\\Temp\\rust_mozprofile.CSQe58I2OCxk\\search.json.mozlz4", (void 0)))
1631259834207 Marionette INFO Listening on port 59932
1631259834612 RemoteAgent WARN TLS certificate errors will be ignored for this session
Sep 10, 2021 1:13:54 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
You should first create a Firefox profile and then embed it to selenium code. You can use the default profile, but because of previous cashes and unnecessary addons, you'll maybe have 'Java Heap' error in first run. So the first step is:
1. Creating new Firefox profile:
1.1. Hold down the Windows Key and Press R on your keyboard and write firefox.exe -p
1.2. Click Create profile button on the opened windows and use wizard to create a new profile (In my case I created MyCreatedProfile)
Now select your new created profile and click on Start Firefox
button
2. Install addons:
On Firefox window search the addons you want and install the addon, I searched for discard-tab
and installed it. The installed addon is now on your created profile.
3. Write code in selenium webDriver:
This code should open Firefox with defined profile(Which the addons is installed on it)
System.setProperty("webdriver.gecko.driver", "c:/geckodriver.exe");
ProfilesIni profilesIni = new ProfilesIni();
FirefoxProfile profile = profilesIni.getProfile("MyCreatedProfile");
FirefoxOptions firefoxOptions= new FirefoxOptions();
firefoxOptions.setProfile(profile);
WebDriver driver = new FirefoxDriver(firefoxOptions);
try {
driver.get("https://google.com/");
Thread.sleep(60000);
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
Note that in this method you do not need to install each extension every time you create new Firefox driver. It helps specially when you need to have multiple extensions.
You can try the source code pushed in Github