Running a specific Firefox profile with Selenium 3

I'm trying to upgrade from Selenium 2 to Selenium 3, but the old processing, which was pretty simple and fast, no longer works (and the documentation doesn't exist as it seems)

This is the program at the moment, and I want to open the Firefox driver with the profile: SELENIUM

Unfortunately, this does not work and always shuts down with an error:

An unhandled exception of type 'System.InvalidOperationException' occurred in WebDriver.dll

Additional information: corrupt deflate stream

This is my program at the moment:

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

Without loading the profile, it only works with the new FirefoxDriver (_service), but the profile is required.

In Selenium 2, I processed it using this code:

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

Quick and easy, but since the driver does not support the constructor with the service and profile, I really do not know how to make it work, any help will be appreciated

+6
1

​​ .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;
+4

Source: https://habr.com/ru/post/1016349/


All Articles