Selenium WebDriver 默认使用哪个配置文件?

17

Selenium WebDriver(又称Selenium 2)在打开FirefoxDriver时使用的匿名配置文件从何处获取?如果它使用的是Firefox的默认配置文件目录%appdata%/roaming/mozilla/firefox/profiles,那么如果我禁用了Firefox插件,那么对于Selenium WebDriver也应该被禁用,那为什么没有呢?


1
据我所知,它总是全新的。你应该创建自己的并根据需要进行修改。 - devsnd
2
好的,这是一个全新的项目,但它仍然基于计算机上存储的某个原型,因为它记得启动一个在 Firefox 中已禁用的插件。这个原型在哪里? - Michael Roller
4
我猜这是firefox提供的一个选项,如果你在命令行中指定了"-CreateProfile"选项,例如"firefox -CreateProfile test"。因此,你要问的问题更多是关于firefox的问题,而不是selenium的问题。我仍然建议使用配置文件管理器创建一个新的配置文件,例如"firefox.exe -ProfileManager"。 - devsnd
在无头模式下安装/启动类似于“xvfb”(帧缓冲服务器)的东西,然后您将能够使用“DISPLAY =:11.0 firefox-CreateProfile test”。 - daniel.kahlenberg
4个回答

21

我来回答这个问题,并支持@twall的评论:在使用Selenium 2 WebDriver启动Firefox时,它会启动一个新的匿名配置文件。

但是,如果您想要更改它,您可以创建新的Firefox配置文件并命名为某种方式,你知道它是什么 - 例如SELENIUM

然后在您的代码中执行以下操作:

 ProfilesIni profile = new ProfilesIni();
 FirefoxProfile ffprofile = profile.getProfile("SELENIUM");
 WebDriver driver = new FirefoxDriver(ffprofile);

这样,Firefox将始终启动该配置文件。您可以在该配置文件中进行所有所需的设置。


这是什么类型的代码?也就是说,我正在使用capybara和selenium-webdriver宝石通过Rails中的selenium。 - Dogweather
我能够像使用多个用户登录一样使用这个配置文件吗?只用单个浏览器?@PavelJanicek - gumuruh

5
您可以为每个Selenium Grid 2节点分配特定的firefox配置文件:

java -jar selenium-server-standalone-2.37.0.jar -Dwebdriver.firefox.profile=my-profile -role node -hub http://example-server.org:4444/grid/register

请注意,webdriver.firefox.profile的值必须是Firefox配置文件的名称,而不是位置或文件夹名称。

2
当在没有创建机器配置文件选项的测试服务器上运行webdriver时,您可以编程方式创建配置文件:
private FirefoxProfile GetFirefoxProfile()
{
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    firefoxProfile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "http://localhost");
    return firefoxProfile;
}

1
获取一个配置文件并不有用,因为它会在内部创建另一个已获取的命名配置文件的副本。如果需要将测试覆盖数据写入跨多次调用的数据存储,则需要访问原始配置文件。
以下是 Selenium 的 ProfilesIni 类的可能解决方案:
首先使用 firefox -p 创建自定义配置文件,例如“CustomSeleniumProfile”。
ProfilesIni profileini = new ProfilesIni() {
    @Override
    public FirefoxProfile getProfile(String profileName) {
            File appData = locateAppDataDirectory(Platform.getCurrent());
            Map<String, File> profiles = readProfiles(appData);
            File profileDir = profiles.get(profileName);
            if (profileDir == null)
              return null;
            return new FirefoxProfile(profileDir);
     }
};
FirefoxProfile profile = profileini.getProfile("CustomSeleniumProfile");
//profile.setEnableNativeEvents(false);

driver = new FirefoxDriver(profile);
//ffDriver.manage().deleteAllCookies();
driver.get("http://www.google.com");

哇,谢谢。现有的ProfilesIni()由于某些原因停止工作,并始终返回/tmp下的匿名配置文件目录。 - philwalk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接