当前位置: 首页 > 面试题库 >

使用Selenium 3启动特定的Firefox配置文件

国晟睿
2023-03-14
问题内容

我正在尝试从Selenium 2升级到Selenium 3,但是旧的处理方式(既简单又快速)不再起作用了(而且似乎不存在该文档)

这是当前的程序,我要打开带有以下配置文件的Firefox驱动程序:SELENIUM

遗憾的是,它无法正常工作,并始终因错误而关闭:

WebDriver.dll中发生类型为’System.InvalidOperationException’>的未处理异常

附加信息:损坏的放气流

这是我目前的程序:

public Program()
{
    FirefoxOptions _options = new FirefoxOptions();
    FirefoxProfileManager _profileIni = new FirefoxProfileManager();
    FirefoxDriverService _service = FirefoxDriverService.CreateDefaultService(@"C:\Programme\IMaT\Output\Release\Bin");
    _service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
    try
    {
        if ((_options.Profile = _profileIni.GetProfile("SELENIUM")) == null)
        {
            Console.WriteLine("SELENIUM PROFILE NOT FOUND");
            _profile.SetPreference("network.proxy.type", 0); // disable proxy
            _profile = new FirefoxProfile();
        }
    }
    catch
    {
        throw new Exception("Firefox needs a Profile with \"SELENIUM\"");
    }
    IWebDriver driver = new FirefoxDriver(_service,_options,new System.TimeSpan(0,0,30));        
    driver.Navigate().GoToUrl("ld-hybrid.fronius.com");
    Console.Write("rtest");
}

static void Main(string[] args)
{
    new Program();
}

在不加载配置文件的情况下,它仅适用于新的FirefoxDriver(_service),但配置文件是必需的。

在Selenium 2中,我使用以下代码进行了处理:

FirefoxProfileManager _profileIni = new FirefoxProfileManager();
// use custom temporary profile
try { 
    if ((_profile = _profileIni.GetProfile("SELENIUM")) == null)
    {
        Console.WriteLine("SELENIUM PROFILE NOT FOUND");
        _profile.SetPreference("network.proxy.type", 0); // disable proxy
        _profile = new FirefoxProfile();
    }
}
catch
{
    throw new Exception("Firefox needs a Profile with \"SELENIUM\"");
}

_profile.SetPreference("intl.accept_languages", _languageConfig);
_driver = new FirefoxDriver(_profile);

快速简单,但是由于驱动程序不支持带有服务和配置文件的构造方法,我真的不知道如何使它正常工作,因此不胜感激


问题答案:

此异常是由于.Net库中的错误所致。生成配置文件的Zip的代码无法提供适当的Zip。

解决此问题的一种方法是重载FirefoxOptions并使用.Net框架(System.IO.Compression.ZipArchive)中的存档器,而不是出现故障ZipStorer

var options = new FirefoxOptionsEx();
options.Profile = @"C:\Users\...\AppData\Roaming\Mozilla\Firefox\Profiles\ez3krw80.Selenium";
options.SetPreference("network.proxy.type", 0);

var service = FirefoxDriverService.CreateDefaultService(@"C:\downloads", "geckodriver.exe");

var driver = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1));



class FirefoxOptionsEx : FirefoxOptions {

    public new string Profile { get; set; }

    public override ICapabilities ToCapabilities() {

        var capabilities = (DesiredCapabilities)base.ToCapabilities();
        var options = (IDictionary)capabilities.GetCapability("moz:firefoxOptions");
        var mstream = new MemoryStream();

        using (var archive = new ZipArchive(mstream, ZipArchiveMode.Create, true)) {
            foreach (string file in Directory.EnumerateFiles(Profile, "*", SearchOption.AllDirectories)) {
                string name = file.Substring(Profile.Length + 1).Replace('\\', '/');
                if (name != "parent.lock") {
                    using (Stream src = File.OpenRead(file), dest = archive.CreateEntry(name).Open())
                        src.CopyTo(dest);
                }
            }
        }

        options["profile"] = Convert.ToBase64String(mstream.GetBuffer(), 0, (int)mstream.Length);

        return capabilities;
    }

}

并通过名称获取配置文件的目录:

var manager = new FirefoxProfileManager();
var profiles = (Dictionary<string, string>)manager.GetType()
    .GetField("profiles", BindingFlags.Instance | BindingFlags.NonPublic)
    .GetValue(manager);

string directory;
if (profiles.TryGetValue("Selenium", out directory))
    options.Profile = directory;


 类似资料:
  • 问题内容: 我尝试使用指定的配置文件启动Firefox: -这个目录是正确的Firefox配置文件目录,我用 但是当我通过selenium检查firefox中的about:cache页面时,它具有不同的缓存路径: 如果通过firefox-bin –ProfileManager运行firefox并选择配置文件,它将显示在about:cache页面正确路径中 为什么WebDriver忽略了Firefo

  • 问题内容: 我正在Eclipse中运行Selenium测试,但是无法加载自定义的Firefox配置文件。 大多数消息来源建议我需要像这样启动Selenium Server: 但是,当从Eclipse中启动我的测试时,它不会使用它-如果Selenium Server未运行,则测试将运行。 但是Selenium IDE(Firefox插件)为我生成的代码未使用该构造函数: 我应该在哪里设置Defaul

  • 我已经使用下面的命令启动了selenium服务器,我想使用定制的firefox配置文件。但是,它如何为我运行的每个测试用例在/tmp/目录中创建一个新的firefoxprofile呢。 /usr/bin/java-jar/usr/local/groundwork/selenium/java/selenium-server-standalone-2.33.0。jar-firefoxProfileTe

  • 本文向大家介绍解决python selenium3启动不了firefox的问题,包括了解决python selenium3启动不了firefox的问题的使用技巧和注意事项,需要的朋友参考一下 selenium3.0之后的版本的就不支持直接打开火狐浏览器,启动火狐浏览器报错,如下图,要想运行就需要我们单独装上驱动。 3.0之前的版本,是可以直接打开火狐浏览器的。 解决办法: 1、首先下载最新版的火狐

  • 问题内容: 我的配置文件位于: 我像这样开始redis: 如何启动Redis,以便它使用我的配置文件? 另外,我讨厌与试图找到一个将其关闭的pid混为一谈。如何通过进入根目录并仅运行一个命令来关闭服务器? 使用puma应用程序服务器,您可以运行以下命令: 并从conf推断pid。Redis可以一样吗? 另外,我还使用了来自redis网站的此复制粘贴的conf: 我已经对其进行了调整,以使其在启动时

  • 我有我在jboss中运行的Spring启动应用程序(而不是tomcat)。我正在使用Spring配置文件来加载环境特定的application.properties. 配置文件特定的应用程序{env}。属性放在“src/main/resources/”下时工作正常,但放在外部时不工作。 我尝试在jboss standalone中设置属性。xml,但应用程序无法在jboss中启动。 请建议如何加载特