javaselenium-webdriverfirefoxselenium-firefoxdriverfirefox-profile

Is there a way to create new named Firefox profiles programmatically with Selenium or some other java APIs?


I'm looking a way to create a new profile programmatically, but can't find is there any specific API or any plain solution like creation specific files in specific folders and so on. Has someone any experience with this problem?


Solution

  • Something like this:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class NewFirefoxProfilesCreator {
        public static void main(String[] args) {
            int end =100;
            int start = 1;
            for (int i=start; i<=end; i++) {
                try {
    
                    Process process = Runtime.getRuntime().exec("\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\" -no-remote -CreateProfile rnn"+i);
    
                    StringBuilder output = new StringBuilder();
    
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(process.getInputStream()));
    
                    String line;
                    while ((line = reader.readLine()) != null) {
                        output.append(line + "\n");
                    }
    
                    int exitVal = process.waitFor();
                    if (exitVal == 0) {
                        System.out.println("Success!");
                        System.out.println(output);
                    } else {
                        //abnormal...
                    }
    
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    100 Firefox profiles are created in a loop.