首页 > 解决方案 > 有没有办法使用 Selenium 或其他一些 java API 以编程方式创建新的命名 Firefox 配置文件?

问题描述

我正在寻找一种以编程方式创建新配置文件的方法,但找不到任何特定的 API 或任何简单的解决方案,例如在特定文件夹中创建特定文件等。有人遇到过这个问题吗?

标签: javaselenium-webdriverfirefoxselenium-firefoxdriverfirefox-profile

解决方案


像这样的东西:

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 配置文件。


推荐阅读