当我们写这样的东西时:
FirefoxProfile ffprofile = new FirefoxProfile(new File("D:\\Selenium"));
这是否意味着我们正在创建一个新的个人资料?因为我无法在 Firefox 配置文件部分找到任何新的配置文件。
现在我的问题是,如何为 Firefox 浏览器创建新的配置文件?
您所说的方法调用只是从给定的配置文件信息目录创建一个 java 配置文件对象,然后通过 WebDriver 实例传递到 Firefox。
为了让 Firefox 保留您的驱动程序并使其可从配置文件管理器中使用,您需要编辑文件profiles.ini,在我的(Windows 7)机器上,该文件位于:
%APPDATA%\漫游\Mozilla\Firefox
此文件夹中的 Profiles 目录包含现有 Firefox 配置文件的存储,当您想要使用现有配置文件作为新配置文件的模板时,复制这些文件非常方便。
您的里程可能会因您的操作系统而异,但我相信您可以通过快速搜索找到它。使用您的示例,然后将以下内容添加到此文件中(其中标题中的 N 是下一个未使用的配置文件编号):
[ProfileN]
Name=selenium
IsRelative=0
Path=D:\Selenium
这将导致 Firefox 配置文件管理器加载配置文件,并允许您使用此配置文件手动启动 Firefox 来配置或测试它,我想这就是您想要做的。
以这种方式创建命名配置文件后,您可以将其分配给 Selenium 中的驱动程序,如下所示:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("selenium");
WebDriver driver = FirefoxDriver(profile);
其中“selenium”与profiles.ini文件中的Name属性相同。
您无法使用 Selenium 创建 Firefox 的配置文件。您可以做的是从 Firefox 中的可用配置文件为您的网络驱动程序创建一个 Firefox 配置文件。 Firefox 个人资料这个词在这里听起来有点含糊。
要在浏览器中创建 Firefox 配置文件,请参阅 Mozilla 支持 页面了解详细信息。
以下代码将创建 Firefox 配置文件(基于提供的文件)并创建一个加载此配置文件的新 FF Webdriver 实例:
FirefoxProfile profile = new FirefoxProfile(new File("D:\\Selenium Profile"));
WebDriver driver = new FirefoxDriver(profile);
也许可以看看 FF 配置文件管理器的官方支持页面 或者在这里:Selenium 的自定义 Firefox 配置文件,了解 FF 配置文件的一些想法。
这是我使用 selenium 3 和 geckodriver 所做的事情:
使用firefox命令行界面创建配置文件
firefox.exe -CreateProfile“profile_name profile_dir” (在java中,通过Runtime.getRuntime().exec函数执行这个运行时)
在 Firefox 选项中设置 -profile 参数
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-profile", <profile_dir>);
driver = new FirefoxDriver(options);
在 Firefox 浏览器中创建个人资料。
这是使用新创建的 Firefox 配置文件的代码。
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("firefox profile name");
WebDriver driver = new FirefoxDriver(myprofile);