I'm trying to create a Node program that uses Selenium and the geckodriver. It should use a specific Firefox profile. It should function like this:
If the Firefox profile isn't open (including if Firefox is already open, but open on a different profile):
- Geckodriver will open Firefox instance and load the Firefox profile. Then run the specified automation tasks.
If the Firefox profile has already been manually opened (by running "firefox" in the terminal, clicking icon, etc):
- Geckodriver will connect to profile already running, then run specified tasks.
Most of the solutions online are for connecting to an existing instance originally started by Selenium. They also aren't for the Javascript API.
I want to stress - I am looking for a solution to let geckodriver access and use a Firefox instance not initially opened by Selenium.
Please give solutions using the Javascript API. You can include additional languages for others to reference, but I'm using the Javascript API.
Here is some of my code so far:
// Modules
const webdriver = require("selenium-webdrive");
const firefox = require("selenium-webdriver/firefox");
// Set Firefox options
const firefoxProfile = "/* FIREFOX PROFILE FILEPATH HERE */"
const firefoxOptions = new firefox.Options();
firefoxOptions.setProfile(firefoxProfile);
// Build driver
const driver = new webdriver.Builder()
.forBrowser("firefox")
.setFirefoxService( new firefox.ServiceBuilder("./node_modules/geckodriver/bin/geckodriver") ) // I installed Geckodriver through npm
.setFirefoxOptions()
.build();
// Run this code
(async function() {
try {
await driver.get("/* TEST URL HERE */");
} catch(err) {
console.log(err);
}
})();
This will launch the correct Firefox profile if it isn't already running. However, it doesn't work if the Firefox profile has already been launched.
It seems like I might be able to do something if I launch Firefox with "firefox -maronette". However, it seems like I also need to find some way to pass the flag "--connect-existing" to Maronette. I can't figure out how to do this.
Please help. Thanks.