【问题标题】:How to create profile in Firefox using Selenium WebDriver如何使用 Selenium WebDriver 在 Firefox 中创建配置文件
【发布时间】:2013-10-16 16:00:15
【问题描述】:

当我们这样写时:

FirefoxProfile ffprofile = new FirefoxProfile(new File("D:\\Selenium"));

这是否意味着我们正在创建一个新的个人资料?因为我将无法在 Firefox 配置文件部分找到任何新配置文件。

所以现在我的问题是,如何为 Firefox 浏览器创建新的配置文件?

【问题讨论】:

    标签: selenium selenium-webdriver profile


    【解决方案1】:

    这是我在 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 驱动程序无法处理代理身份验证对话框的解决方案。它没有实现 HasCredentials 接口,当我们使用 driver.get(url) 时会阻止代理身份验证对话框出现。当出现此对话框时,selenium 会引发异常。我尝试使用 sendKeys 填写用户名和密码,但它抛出另一个异常,说不支持对话身份验证发送密钥。
    【解决方案2】:

    在 Firefox 浏览器中创建配置文件。

    这是使用新创建的 firefox 配置文件的代码。

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile myprofile = profile.getProfile("firefox profile name");
    WebDriver driver = new FirefoxDriver(myprofile);
    

    【讨论】:

      【解决方案3】:

      您无法使用 Selenium 创建 Firefox 配置文件。您可以做的是从 firefox 中的可用配置文件为您的 webdriver 创建一个 firefox 配置文件。 Firefox 配置文件的词在这里听起来有点模棱两可。

      要在浏览器中创建 firefox 配置文件,请参阅Mozilla support 页面了解详细信息。

      【讨论】:

        【解决方案4】:

        您所说的方法调用只是从给定的配置文件目录中创建一个 java 配置文件对象,然后通过 WebDriver 实例将其传递给 Firefox。

        为了让 Firefox 持久保存您的驱动程序并使其在配置文件管理器中可用,您需要在我的 (Windows 7) 机器上编辑文件 profiles.ini:

        %APPDATA%\Roaming\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 属性相同。

        【讨论】:

        • ProfilesIni 如何知道路径?我们只是给出名字......因为profiles.ini的路径因系统而异。
        • 您系统的 selenium firefox 驱动程序实现知道如何为您的 Firefox 特定系统安装找到profiles.ini。然后,您在profiles.ini 中定义存储您的配置文件数据的路径,您可以将其配置为查看系统上的任何位置。
        • getProfile 函数创建给定配置文件的副本。因此,在此配置文件中运行不会像普通配置文件那样存储值(例如,记住密码不起作用等)
        • profile.ini 的正确路径是 %APPDATA%\Mozilla\Firefox。 APPDATA 已经包含\Roaming
        【解决方案5】:

        以下代码将创建 firefox 配置文件(基于提供的文件)并创建一个加载此配置文件的新 FF webdriver 实例:

        FirefoxProfile profile = new FirefoxProfile(new File("D:\\Selenium Profile"));                  
        WebDriver driver = new FirefoxDriver(profile);
        

        也许可以查看official support page 的 FF 配置文件管理器 或在这里:Custom Firefox profile for Selenium 了解 FF 配置文件。

        【讨论】:

        • 我使用了上面的代码,但是当我使用“firefox.exe -ProfileManager -no-remote”命令搜索新配置文件时,它没有显示任何新配置文件
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        • 2018-06-21
        • 2021-05-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多