首页 文章

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

提问于
浏览
8

我正在尝试从Selenium 2升级到Selenium 3,但旧处理非常容易和快速不再起作用(并且文档似乎不存在)

这是目前的程序,我想要的是打开一个带有配置文件的Firefox驱动程序:SELENIUM

可悲的是,它不起作用,并始终关闭错误:

WebDriver.dll中发生未处理的类型'System.InvalidOperationException'>异常附加信息:corrupt deflate stream

这是我目前的计划:

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);

快速而简单,但由于驱动程序不支持带有服务和配置文件的构造函数,我真的不知道如何使其工作,任何帮助将不胜感激

1 回答

  • 4

    此异常是由.Net库中的错误引起的 . 生成配置文件的Zip的代码无法提供正确的Zip .

    解决此问题的一种方法是重载 FirefoxOptions 并使用.Net framework(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;
    

相关问题