The type initializer for 'System.IO.Compression.ZipStorer' threw an exception when loading existing FirefoxProfile through GeckoDriver Selenium and C#

2

Error

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'System.IO.Compression.ZipStorer' threw an exception.
  Source=WebDriver
  StackTrace:
   at System.IO.Compression.ZipStorer.WriteLocalHeader(ZipFileEntry& zipFileEntry)
   at System.IO.Compression.ZipStorer.AddStream(CompressionMethod compressionMethod, Stream sourceStream, String fileNameInZip, DateTime modificationTimeStamp, String fileEntryComment)
   at System.IO.Compression.ZipStorer.AddFile(CompressionMethod compressionMethod, String sourceFile, String fileNameInZip, String fileEntryComment)
   at OpenQA.Selenium.Firefox.FirefoxProfile.ToBase64String()
   at OpenQA.Selenium.Firefox.FirefoxOptions.GenerateFirefoxOptionsDictionary()
   at OpenQA.Selenium.Firefox.FirefoxOptions.ToCapabilities()
   at OpenQA.Selenium.Firefox.FirefoxDriver.ConvertOptionsToCapabilities(FirefoxOptions options)
   at OpenQA.Selenium.Firefox.FirefoxDriver..ctor(FirefoxDriverService service, FirefoxOptions options, TimeSpan commandTimeout)
   at OpenQA.Selenium.Firefox.FirefoxDriver..ctor(FirefoxOptions options)
   at linkedin_mp.Controllers.LinkedInController.Get2() in E:\github\donhuvy\linkedin_crawler\Controllers\LinkedInController.cs:line 208
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
NotSupportedException: No data is available for encoding 437. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

enter image description here

source code

/// <summary>
/// Get a specific profile information.
/// URL: https://localhost:5001/linkedin/getprofile
/// </summary>
/// <returns>Danh sách các string là URL profile LinkedIn.</returns>
[HttpGet]
[Route("getprofile")]
public IEnumerable<string> Get2()
{
    List<string> list = new List<string>();
    Console.OutputEncoding = System.Text.Encoding.UTF8;
    // Bắt buộc phải nạp Profile đang sử dụng.
    // string PROFILE_DIR = "D://tmp//linkedin_tien";            
    FirefoxOptions firefoxOptions = new FirefoxOptions();
    FirefoxProfile firefoxProfile = new FirefoxProfile("C:\\Users\\MinhPhuc\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\tiqq1wks.dev-edition-default\\");
    // FirefoxProfile firefoxProfile = new FirefoxProfile();
    // firefoxProfile.SetPreference("permissions.default.image", 2);
    // firefoxProfile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
    firefoxProfile.DeleteAfterUse = false;
    firefoxOptions.Profile = firefoxProfile;            
    // firefoxOptions.SetPreference("permissions.default.stylesheet", 2);
    // firefoxOptions.SetPreference("javascript.enabled", false);
    // firefoxOptions.SetPreference("dom.ipc.plugins.enabled.libflashplayer.so", false);
    using IWebDriver driver = new FirefoxDriver(firefoxOptions);
    // IWebDriver driver = new FirefoxDriver(firefoxOptions);
    WebDriverWait webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    driver.Navigate().GoToUrl("https://www.linkedin.com/");
c#
selenium
selenium-webdriver
geckodriver
firefox-profile
asked on Stack Overflow Jul 14, 2020 by Do Nhu Vy • edited Jul 14, 2020 by DebanjanB

1 Answer

3

The FirefoxProfile Constructor (String) initializes a new instance of the FirefoxProfile class using a specific profile directory.

So while using GeckoDriver and existing FirefoxProfile() instead of:

FirefoxProfile firefoxProfile = new FirefoxProfile("C:\\Users\\MinhPhuc\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\tiqq1wks.dev-edition-default\\");

You need to remove the trailing slashes \\ as follows:

FirefoxProfile firefoxProfile = new FirefoxProfile("C:\\Users\\MinhPhuc\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\tiqq1wks.dev-edition-default");

References

You can find a couple of relevant detailed discussions in:

answered on Stack Overflow Jul 14, 2020 by DebanjanB • edited Jul 14, 2020 by DebanjanB

User contributions licensed under CC BY-SA 3.0