【问题标题】:What profile does Selenium WebDriver use by default?Selenium WebDriver 默认使用什么配置文件?
【发布时间】:2012-08-20 01:09:30
【问题描述】:

Selenium WebDriver(又名 Selenium 2)在哪里获取它在打开 FirefoxDriver 时使用的匿名配置文件?如果它使用 Firefox 的默认值 %appdata%/roaming/mozilla/firefox/profiles,那么如果我要禁用一个 firefox 插件,那么 Selenium WebDriver 也应该禁用它,那为什么不呢?

【问题讨论】:

  • AFAIK 它始终是全新的。您应该创建自己的并根据需要进行修改。
  • 好吧,它是一个全新的,但它仍然基于存储在计算机上的一些原型,因为它记得启动一个在 Firefox 中被禁用的插件。这个原型在哪里?
  • 如果您在命令行上指定-CreateProfile 选项,我猜它是firefox 提供的。例如firefox -CreateProfile test。所以你要问的是一个firefox问题,而不是一个硒问题。我仍然建议使用配置文件管理器创建一个新的配置文件,例如firefox.exe -ProfileManager
  • 在无头情况下安装/启动类似xvfb(帧缓冲服务器)的东西,您将能够使用DISPLAY=:11.0 firefox -CreateProfile test

标签: firefox selenium selenium-webdriver


【解决方案1】:

我会回答它,支持来自@twall 的评论:在 Selenium 2 WebDriver 中启动 firefox 时,它会启动新的匿名配置文件。

但是,如果你想改变它,你可以create new Firefox profile 并以某种方式命名它,你知道它是什么 - 例如SELENIUM

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

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

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

【讨论】:

  • 这是什么代码?即,我通过 capybara 和 selenium-webdriver gems 在 Rails 中使用 selenium。
  • 我可以像使用单个浏览器使用多个用户登录一样使用此配置文件吗? @PavelJanicek
【解决方案2】:

您可以为每个 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 配置文件名称,而不是位置或文件夹名称

【讨论】:

    【解决方案3】:

    当在测试服务器上运行 webdriver 而没有在机器上创建配置文件的选项时,您可以通过编程方式创建配置文件:

    private FirefoxProfile GetFirefoxProfile()
    {
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        firefoxProfile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "http://localhost");
        return firefoxProfile;
    }
    

    【讨论】:

      【解决方案4】:

      获取配置文件没有用,因为它在内部创建了获取的命名配置文件的另一个副本。如果出现以下情况,则需要访问原始配置文件 例如:测试覆盖率数据应该跨多次调用写入数据存储。

      这是一个可能的解决方案,方法是覆盖 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 下返回一个匿名配置文件目录。
      猜你喜欢
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 2014-11-04
      相关资源
      最近更新 更多