Selenium Firefox Profilo: Guida all'installazione
โก Riepilogo intelligente
Firefox il profilo memorizza i segnalibri, le password, i componenti aggiuntivi e le preferenze che appartengono a un utente del browser e Selenium WebDriver can load a dedicated profile so every automated run starts from an identical, predictable browser state.
Selenium Firefox Profile
Firefox profile รจ la raccolta di impostazioni, personalizzazioni, componenti aggiuntivi e altre impostazioni di personalizzazione che possono essere eseguite su Firefox Browser. Puoi personalizzare Firefox profilo adatto al tuo Selenium requisito di automazione.
Inoltre Firefox o qualsiasi altro browser gestisce le impostazioni dei certificati SSL. Quindi automatizzarli ha molto senso insieme al codice di esecuzione del test.
In short a profile is a userโs personal settings. When you want to run a reliable automazione su una Firefox browser, it is recommended to make a separate profile. A dedicated profile also keeps your everyday browsing data out of the test run, which is the first thing to rule out when a Selenium script behaves differently on two machines.
Posizione della cartella del profilo nel disco
Firefox profile รจ proprio come i diversi utenti che utilizzano Firefox. Firefox saves personal information such as bookmarks, passwords, and user preferences which can be edited, deleted or created using the profile manager.
The screenshot below shows a profile folder as it appears on disk, named after the profile with a random prefix.
La posizione del profilo รจ la seguente
- Per Windows > C:\Users\<username>\AppData\Roaming\Mozilla\Firefox\Profiles\profile_name.default
- Per Linux > ~/.mozilla/firefox/profile_name.default/
- For Mac OS X > ~/Library/Application Support/Firefox/Profili/nome_profilo.default/
Per eseguire un successo Selenium Prova, A Firefox il profilo dovrebbe essere โ
- Facile da caricare
- Impostazioni proxy, se necessario
- Altre impostazioni specifiche dell'utente in base alle esigenze di automazione
Come impostare Firefox Profilo per Selenium Test
Let us see step by step how to create a Firefox profilo.
Passo 1) Chiudi il Firefox del browser
In the first step, close Firefox if it is already open. The profile manager will not start while a Firefox window is running.
Passo 2) Apri corsa (Windows tasto + R) e digitare firefox.exe โp
The Run dialog should look like the screenshot below before you press OK.
Note: If it does not open you can try using full path enclosed in quotes.
- On 32 bit Windows: โC:\Program Files\Mozilla Firefox\firefox.exeโ โp
- On 64 bit Windows (32 bit Firefox): โC:\Program Files (x86)\Mozilla Firefox\firefox.exeโ โp
Passo 3) Scegli il profilo utente
Now, a dialogue box named Firefox will open, as shown below.
Passo 4) Crea il tuo profilo
Select the Create Profile option shown in the next screenshot to start the wizard.
Ora seleziona l'opzione Crea profilo dalla finestra e si aprirร una procedura guidata. Fare clic su Avanti.
Passo 5) Dai il nome del tuo profilo
Type a name for the automation profile in the field highlighted below and finish the wizard.
Ora il tuo profilo รจ pronto, puoi selezionare il tuo profilo e aprirlo Firefox. From here the profile behaves like any other browser session, so a primo script WebDriver will run against it unchanged.
Noterai che il nuovo Firefox la finestra non mostrerร nessuno dei segnalibri e delle icone dei preferiti.
Note: The last selected profile, will load automatically at next Firefox lancio. Sarร necessario riavviare il gestore profili se desideri modificare i profili.
Script di automazione per Selenium
With the profile created, the next step is to load it from code. To access newly created Firefox profile in a Selenium WebDriver software test, we need to use WebDriverโs inbuilt class ProfilesIni and its method getProfile as shown below.
โ ๏ธ Nota sulla versione: the snippets in this section are the original Selenium 2 form. In current Selenium releases the class lives in org.openqa.selenium.firefox.ProfilesIni - Il .internal sub-package was dropped โ and the profile is handed to the driver through FirefoxOptions. See the FirefoxOptions section below for the modern equivalent.
Selenium codice per il profilo
This is a code to implement a profile, which can be embedded in the Selenium codice.
ProfilesIni profile = new ProfilesIni();
// questo creerร un oggetto per il Firefox tuo profilo
FirefoxProfile myprofile = profile.getProfile("xyzProfile");
// questo inizializzerร il file Firefox autista
WebDriver driver = new FirefoxDriver(myprofile)
Let us see the implementation of this code in the following examples.
Firefox Esempio di profilo 1
The first example loads a profile by name. The screenshot below shows the same script inside the editor.
// import the package import java.io.File; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; public class FirefoxProfile { public static void main(String[] args) { ProfilesIni profile = new ProfilesIni(); FirefoxProfile myprofile = profile.getProfile("xyzProfile"); // Initialize Firefox driver WebDriver driver = new FirefoxDriver(myprofile); //Maximize browser window driver.manage().window().maximize(); //Go to URL which you want to navigate driver.get("http://www.google.com"); //Set timeout for 5 seconds so that the page may load properly within that time driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //close firefox browser driver.close(); } }
Spiegazione del codice
Di seguito รจ riportata la spiegazione del codice riga per riga.
- Code line 2-7: First of all we need to import the package required to run the Selenium codice.
- Code line 8: Make a public class โFirefoxProfilo."
- Code line 9: Make an object (you need to have basic knowledge of OOPs concepts).
- Code line 10-11: We need to initialize Firefox profile with the object of myprofile.
- Code line 13: Create object for Firefox.
- Code line 15: Maximize window.
- Code line 17: driver.get is used to navigate to the given URL.
- Code line 19: Set timeout is used to wait for some time so that browser may load the page before proceeding to next page.
- Code line 21: Close Firefox.
Let us see one more example.
Firefox Esempio di profilo 2
The second example skips profiles.ini and points at the profile folder directly, as the editor screenshot shows.
import java.io.File; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; public class FirefoxProfile2{ public static void main(String[] args) { // Create object for FirefoxProfile FirefoxProfilemyprofile=newFirefoxProfile (newFile("\c:users\AppData\MozillaFirefoxProfile_name.default ")); // Initialize Firefox driver WebDriver driver = new FirefoxDriver(myprofile); //Maximize browser window driver.manage().window().maximize(); //Go to URL driver.get("http://www.google.com"); //Set timeout driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //close firefox browser driver.close(); }
Spiegazione del codice
Di seguito รจ riportata la spiegazione del codice riga per riga.
- Code line 1-6: First of all we need to import the package required to run the Selenium codice.
- Code line 8: Make a public class FirefoxProfile2.
- Code line 12: Make the object of myprofile by referring to the exact path.
- Code line 14: Create object for Firefox.
- Code line 16: Maximize window.
- Code line 18: driver.get is used to navigate to the given URL.
- Code line 20: Set timeout is used to wait for some time so that browser may load the page before proceeding to next page.
- Code line 22: Close Firefox.
Note that the path in this snippet is written exactly as the original tutorial shows it. On a real machine the backslashes must be escaped, for example โC:\\Users\\guru99\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\xyzProfile.defaultโ.
Come usare a Firefox profili in Selenium 4 con FirefoxOpzioni
Both examples above compile only against Selenium 2. Two things changed afterwards, and every current tutorial uses the newer form.
- ProfilesIni moved out of the interno sub-package, so the import is now org.openqa.selenium.firefox.ProfilesIni.
- Migliori FirefoxDriver(FirefoxProfile) constructor was removed. The profile is attached to a FirefoxOptions object and the options object is passed to the driver.
- Selenium 4 timeouts take a java.time.Duration instead of a value plus a TimeUnit, so implicitlyWait(5, TimeUnit.SECONDS) becomes implicitlyWait(Duration.ofSeconds(5)).
The table below maps each legacy call to its replacement.
| Legacy call (Selenium 2) | Current call (Selenium 3 e 4) |
|---|---|
| import org.openqa.selenium.firefox.internal.ProfilesIni; | import org.openqa.selenium.firefox.ProfilesIni; |
| new FirefoxDriver(myprofile) | options.setProfile(myprofile); new FirefoxDriver(options) |
| implicitlyWait(5, TimeUnit.SECONDS) | implicitlyWait(Duration.ofSeconds(5)) |
| System.setProperty for geckodriver | Resolved automatically by Selenium direttore |
Rewritten against a current release, Example 1 looks like this.
import java.time.Duration; 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 FirefoxProfileSelenium4 { public static void main(String[] args) { ProfilesIni allProfiles = new ProfilesIni(); FirefoxProfile myprofile = allProfiles.getProfile("xyzProfile"); FirefoxOptions options = new FirefoxOptions(); options.setProfile(myprofile); WebDriver driver = new FirefoxDriver(options); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5)); driver.get("https://www.google.com"); driver.quit(); } }
The profile lookup itself is unchanged: ProfilesIni still reads profiles.ini from the Firefox application-data folder and getProfile still returns null when the name does not match a profile, so a null check is worth adding before the driver starts. The same options object also carries the arguments used elsewhere, such as the ones for maximizing or resizing the browser window.
Come impostare Firefox Profile Preferences in Selenium
A saved profile is only half the story. Most automation needs a handful of preferences applied in code so the same behaviour follows the suite onto any machine. The setPreference method writes those values into the profile copy before Firefox lancia.
FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("browser.download.folderList", 2); profile.setPreference("browser.download.dir", "C:\\selenium-downloads"); profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf"); profile.setPreference("dom.webnotifications.enabled", false); profile.setPreference("intl.accept_languages", "es"); FirefoxOptions options = new FirefoxOptions(); options.setProfile(profile); WebDriver driver = new FirefoxDriver(options);
The preferences below cover the cases that come up most often in day-to-day scripting.
| preferenza | Cosa controlla |
|---|---|
| browser.download.folderList | Set to 2 to use a custom download folder instead of the system default. |
| browser.download.dir | The absolute path Firefox saves downloads into. |
| browser.helperApps.neverAsk.saveToDisk | MIME types saved without showing the download dialog. |
| dom.webnotifications.abilitato | Set to false to suppress web push notification prompts. |
| intl.accept_languages | The Accept-Language header, useful for localization runs. |
| general.userage.override | Replaces the user agent string sent by the browser. |
Two limits are worth knowing. Preferences must be set before the driver is created, because WebDriver copies the profile at launch and later changes are ignored. WebDriver also reserves a small set of preferences it needs in order to talk to the browser, and it overwrites those whatever you assign. Because those values live in the profile rather than in the script, they also carry across a test su piรน browser run without any change to the test code.
Uncommon Firefox Profile Errors in Selenium e come risolverli
Profile problems usually surface as a compile error or as a browser that opens with the wrong settings. The table lists the failures reported most often, along with what causes each one.
| Sintomo | Causare | Fissare |
|---|---|---|
| The constructor FirefoxDriver(FirefoxProfile) is undefined | The constructor was removed after Selenium 2. | Wrap the profile in FirefoxOptions and pass the options object instead. |
| Cannot resolve import org.openqa.selenium.firefox.internal.ProfilesIni | The class moved out of the internal sub-package. | Import org.openqa.selenium.firefox.ProfilesIni. |
| NullPointerException on getProfile | The profile name does not match any entry in profiles.ini. | Reopen the profile manager and copy the name exactly, including case. |
| Firefox profile cannot be loaded. It may be missing or inaccessible. | Un altro Firefox instance already holds the profile, or the folder path is wrong. | Close every Firefox window, then verify the path against the locations listed above. |
| A preference has no effect | It was set after the driver started, or WebDriver reserves it. | Move every setPreference call above the FirefoxDriver constructor. |
If the browser starts correctly but the script still fails afterwards, the profile is probably not the cause. Working through gestione delle eccezioni in Selenium is usually the faster route from that point.







